how to POST contents of JSON file to RESTFUL API with Python using requests module

前端 未结 4 1679
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-25 15:30

Okay, I give up. I am trying to post the contents of a file that contains JSON. The contents of the file look like this:


{
     \"id”:99999999,
              


        
4条回答
  •  死守一世寂寞
    2020-12-25 15:48

    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)
    

提交回复
热议问题