Convert cURL request to Python-Requests request
The Curl command sends specific multipart/form-data
field names; meta
and data
, and the documentation for the API specifies specific meta-types to be used.
Moreover, the metadata should be encoded to JSON.
The following should work:
import jsonimport requestsmetadata = json.dumps({"documentType": "application/msword"})files = { 'meta': ('', metadata, 'application/json'), 'data': ('sample.docx', content, 'application/octet-stream'),}req = requests.post( "https://text.s4.ontotext.com/v1/twitie", auth=("user", "pass"), files=files)
The files
parameter is all that is needed here; each value is a tuple with a filename, the data to be sent, and the mimetype for that part.
One good resource I've used for converting a cURL request to Python requests is curlconverter. You can enter your cURL request and it will format it for Python requests
.
As a side note, it can also convert for PHP and Node.js.