How to Post JSON information in Python console

In this post, I am going to show how to post JSON information in 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 Python console.

Then, let’s execute the code.

Output

This is image title

Thank you for reading, keep visitting!

#python #JSON

How to Post JSON information in Python console
12.30 GEEK