Introduction
In this blog, I am going to show how to post JSON information in the Python console.
Software requirement
Python 3.5 and IDLE (Python 3.5)
Programming code
- #Posting json in Python
- #import packages
- import requests
- import simplejson as json
- #variable
- url="https://www.googleapis.com/urlshortener/v1/url"
- #Payload
- payload={"longUrl": "http://www.google.com/"}
- #Headers
- headers={"Content-Type: application/json"}
- #Request
- r= requests.post(url, json=payload)
- #Output statement
- print(r.text)
About the code
First, I am importing the requests and simplejson modules.
Next, setting a variable and assigning the payload, headers value.
Finally, I have written the request and output statement to show the result in the Python console.
Then, let’s execute the code.
Output


Join the conversation! Your thoughts help the community grow.