how to POST contents of JSON file to RESTFUL API with Python using requests module
This should work, but it's meant for very large files.
import requests
url = 'https://api.example.com/api/dir/v1/accounts/9999999/orders'
headers = {'Authorization' : ‘(some auth code)’, 'Accept' : 'application/json', 'Content-Type' : 'application/json'}
r = requests.post(url, data=open('example.json', 'rb'), headers=headers)
If you want to send a smaller file, send it as a string.
contents = open('example.json', 'rb').read()
r = requests.post(url, data=contents, headers=headers)
First of all your json file does not contain valid json. as in,"id”
-here the closing quotation mark is different than the opening quotation mark. And other ID fields have the same error. Make it like this "id"
.
now you can do it like this,
import requests
import json
with open('example.json') as json_file:
json_data = json.load(json_file)
headers = {'Authorization' : ‘(some auth code)’, 'Accept' : 'application/json', 'Content-Type' : 'application/json'}
r = requests.post('https://api.example.com/api/dir/v1/accounts/9999999/orders', data=json.dumps(json_data), headers=headers)