How to POST multipart/form-data in Python with requests
try with this
import requests
headers = {
'Content-Type': 'multipart/form-data',
'x-api-key': 'xxxxxx-xxxxx-xxxx-xxxx-xxxxxxxx',
}
files = {
'file': ('<image>.jpg', open('<image>.jpg', 'rb')),
}
response = requests.post('http://localhost:8000/api/v1/recognition/recognize', headers=headers, files=files)
Upload file via POST multipart form-data using Python
Just post it without stream bytes and header magic:
file = {'BackupFile': open(file_name, 'rb')}
response = requests.post(url, files=file)
all the rest will make requests
: will send a multi-part form POST body with the BackupFile
field set to the contents of the file.txt
file.
Also the filename will be included in the mime header for the specific field.
example:
>>> files = {'BackupFile': open('file.txt', 'rb')}
>>> print(requests.Request('POST', 'http://example.com', files=files).prepare().body.decode('ascii'))
--0c249de0e6243e92307003732e49ffe9
Content-Disposition: form-data; name="BackupFile"; filename="file.txt"
--0c249de0e6243e92307003732e49ffe9--
How to send a 'multipart/form-data' with requests, without files in Python
I'm not entirely sure how python works with requests. You can try this accepted answers or any which is suitable
python requests - POST Multipart/form-data without filename in HTTP request
requests: post multipart/form-data
Removed 'content-type' from the headers, now it works
try:
headers = {
'authorization': "Basic ZXNlbnRpcmVcY
Related Topics
Best Way to Convert String to Bytes in Python 3
Convert All Strings in a List to Int
Are For-Loops in Pandas Really Bad? When Should I Care
How to Select a Variable by (String) Name
Typeerror: Method() Takes 1 Positional Argument But 2 Were Given
How to Concatenate Text Files in Python
How to Find Overlapping Matches With a Regexp
Django Template How to Look Up a Dictionary Value With a Variable
Convert String to Variable Name in Python
Remove Specific Characters from a String in Python
Pass a List to a Function to Act as Multiple Arguments
Retrieve Links from Web Page Using Python and Beautifulsoup
How to Wait Some Time in Pygame
dddddd",
'cache-control': "no-cache",
}
myfile = {"file": ("filexxx", open(filepath, "rb"))}
response = requests.request("POST", verify=False, url=url, data=myfile, headers=headers)
print(response.text)
except Exception as e:
print "Exception:", e
How to send a “multipart/form-data” with requests in python?
payload = { 'username':'xxxxx',
'passwd':'xxxx'}
session = requests.Session()
req = session.post('https://exemple.com/0',data=payload)
payload ={'check_type_diff':'0',
'category':'19',
'company_ad':'0'}
req = session.post('https://exemple.com/0', data=payload )
print req.content
Note: You should use post('URL',files=files)
if you have file content. The multipart data just works as a normal data, just the formatting and the method is not the same.
Example:
If you have a file and some multipart data, your code will be like this:
files = {"file":(filename1,open(location+'/'+filename,"rb"),'application-type')}
payload ={'file-name':'Filename',
'category':'19'}
req = session.post('https://exemple.com/0', data=payload, file=files)
print req.content
You don't even need to add the line "file" into the payload
, the requests
will put the request together.
Related Topics
Best Way to Convert String to Bytes in Python 3
Convert All Strings in a List to Int
Are For-Loops in Pandas Really Bad? When Should I Care
How to Select a Variable by (String) Name
Typeerror: Method() Takes 1 Positional Argument But 2 Were Given
How to Concatenate Text Files in Python
How to Find Overlapping Matches With a Regexp
Django Template How to Look Up a Dictionary Value With a Variable
Convert String to Variable Name in Python
Remove Specific Characters from a String in Python
Pass a List to a Function to Act as Multiple Arguments
Retrieve Links from Web Page Using Python and Beautifulsoup
How to Wait Some Time in Pygame