Python requests - POST data from a file

前端 未结 1 606
醉酒成梦
醉酒成梦 2020-12-13 10:10

I have used curl to send POST requests with data from files.

I am trying to achieve the same using python requests module. Here is my python script

i         


        
相关标签:
1条回答
  • 2020-12-13 10:45

    You do not need to use .read() here, simply stream the object directly. You do need to set the Content-Type header explicitly; curl does this when using --data but requests doesn't:

    with open('data','rb') as payload:
        headers = {'content-type': 'application/x-www-form-urlencoded'}
        r = requests.post('https://IP_ADDRESS/rest/rest/2', auth=('userid', 'password'),
                          data=payload, verify=False, headers=headers)
    

    I've used the open file object as a context manager so that it is also auto-closed for you when the block exits (e.g. an exception occurs or requests.post() successfully returns).

    0 讨论(0)
提交回复
热议问题