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

前端 未结 4 1655
佛祖请我去吃肉
佛祖请我去吃肉 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 16:10

    You need to parse the JSON, and pass that the body like so:

    import requests
    import json
    json_data = None
    
    with open('example.json') as json_file:
        json_data = json.load(json_file)
    
    auth=('token', 'example')
    
    r = requests.post('https://api.example.com/api/dir/v1/accounts/9999999/orders', json=json_data, auth=auth)
    

提交回复
热议问题