Python requests - POST data from a file
Tag : python , By : Dasharath Yadav
Date : March 29 2020, 07:55 AM
This might help you 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)
|
Python Requests - Post a zip file with multipart/form-data
Date : March 29 2020, 07:55 AM
will help you As far as requests is concerned, there is no difference between a zip file and any other binary blob of data. Your server is broken here; it is cutting of the connection when you send it a zip file. That is not something requests can do anything about. fileobj = open('/Users/.../test.zip', 'rb')
r = requests.post(url, auth=HTTPDigestAuth('dev', 'dev'), data = {"mysubmit":"Go"}, files={"archive": ("test.zip", fileobj)})
>>> import requests
>>> fileobj = open('/tmp/test.zip', 'rb')
>>> r = requests.post('http://httpbin.org/post', data={"mysubmit":"Go"}, files={"archive": ("test.zip", fileobj)})
>>> r
<Response [200]>
>>> r.json()
{u'origin': u'217.32.203.188', u'files': {u'archive': u'data:application/zip;base64,<long base64 body omitted>'}, u'form': {u'mysubmit': u'Go'}, u'url': u'http://httpbin.org/post', u'args': {}, u'headers': {u'Content-Length': u'57008', u'Accept-Encoding': u'gzip, deflate, compress', u'Connection': u'close', u'Accept': u'*/*', u'User-Agent': u'python-requests/1.2.3 CPython/2.7.5 Darwin/12.4.0', u'Host': u'httpbin.org', u'Content-Type': u'multipart/form-data; boundary=9aec1d03a1794177a38b48416dd4c811'}, u'json': None, u'data': u''}
|
python POST requests with file + data
Date : March 29 2020, 07:55 AM
it fixes the issue Iam trying to upload a picture and a information over an API that requires to be send as a form. I tried to use the "files" option, that requests provides with no success. It gives me the following error: , There are a couple of things to try: open('filename', 'rb')
>>> multiple_files = [('images', ('foo.png', open('foo.png', 'rb'), 'image/png')),
('images', ('bar.png', open('bar.png', 'rb'), 'image/png'))]
>>> r = requests.post(url, files=multiple_files)
|
Using python requests to POST data with file
Date : March 29 2020, 07:55 AM
I hope this helps . You can't just pass in a Python dictionary without encoding it to JSON. Your curl post has JSON-encoded data for the jsonData form field, so your Python code needs to provide the same: import json
headers = {'X-Authorization': '12345'}
files = {
'file0': ('/path/to/image.jpg',
open('/path/to/image.jpg', 'rb')),
}
file_post = requests.post(
'http://example.com/url/path',
headers=headers,
files=files,
data={
"jsonData": json.dumps({
"data": {
"field1": 1,
"field2": 2,
}
})
})
|
Python server is successfully handling post requests, but the file sending post requests fails somehow
Date : March 29 2020, 07:55 AM
around this issue I wasn't sending a proper response code back to the client. Adding this in the do_POST method fixed it. self.send_response(200, "OK")
self.end_headers()
|