Posting JSON In Python

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
  1. #Posting json in Python  
  2. #import packages  
  3. import requests  
  4. import simplejson as json  
  5. #variable  
  6. url="https://www.googleapis.com/urlshortener/v1/url"  
  7. #Payload  
  8. payload={"longUrl""http://www.google.com/"}  
  9. #Headers  
  10. headers={"Content-Type: application/json"}  
  11. #Request  
  12. r= requests.post(url, json=payload)  
  13. #Output statement  
  14. 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