Post a string as multipart/form-data using requests

Dan

I am posting to an API that seems to insist on receiving XML data as multipart/form-data with the name (file name?) xml. It works in postman but I can't get it to work using Python's requests. This is my Python code (based on https://stackoverflow.com/a/24443309/1011724):

requests.post(callpro_url,
              files={'xml':('data.xml',result)},
              verify=False).text

where result is a string containing XML. If I try this code I get the response:

xml post field is empty

which is the response this API give if you don't use the multipart/form-data header.

If I generate code from the working postman post I get something like this (slightly redacted):

import requests

url = "https://blablabla.blablab.com/blabla/api.php"

querystring = {"mode":"import","hash":"redacted-hash","xml":"\"xml\""}

payload = "------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data; name=\"xml\"\r\n\r\n<?xml version=\"1.0\" ?>\n<importdata>\n --redacted-XML-- \n</importdata>\n\r\n------WebKitFormBoundary7MA4YWxkTrZu0gW--"
headers = {
    'content-type': "multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW",
    'cache-control': "no-cache",
    'postman-token': "8d3ec8ee-784e-3a65-5240-cf1a9534d1c4"
    }

response = requests.request("POST", url, data=payload, headers=headers, params=querystring)

print(response.text)

Executing this code in Python, it gives the correct response.

Note that the params=querystring part in the postman code is taken care of in the URL in my code.

I'm confused by the payload in the postman code. It adds things like Content-Disposition and name in the string. I assume that I can put this stuff in the tuple in the files parameter but I'm not sure how to to do it. I've tried files={'xml':('data.xml',result,'form-data')} for example and also files={'xml':('data.csv',result)} and {'xml':('xml',result)}.

Also, the postman code explicitly defines the header as

 'content-type': "multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW"

whereas the requests documentation say that I should never explicitly define the headers in that way. However the postman code works and my code does not.

Any suggestions?

Dan

I don't know if this is worth answering or if I should delete the question but what was needed was to name the file "xml" not xml. so this works:

requests.post(callpro_url,
              files={'xml':('"xml"',result)},
              verify=False).text

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Failed post Image & data Using Requests DRF

Using python requests to POST data with file

Using Python Requests to POST data to REST API

How to pass query string with data using Requests

what is the default encoding when python Requests post data is string type?

Send multipart/form-data string with POST requests, python

How to upload a variable amount of files using a single multipartform post header key

How to pass a string to a post call, using python requests

Upload Image using POST form data in Python-requests

multipart data POST using python requests: no multipart boundary was found

unable to post file+data using python-requests

Send http post request with unicoded data using python requests

How to send a post request with data using python requests module?

Post data to Outlook calendar using Django, Requests, and Microsoft Graph

flask python requests post using JSON data to localhost

python web scraping post form data using requests not working

Handling post data with requests in python

Python: Issue with POST data in Requests

Error to post the data in python requests

Post JSON using Python Requests

Post JSON using Python Requests

Post uploaded file using requests

Sending POST requests using Cookies

AngularJS lost data from multipartForm directive to routes

Using python requests to post - How do I get the correct table data I request?

Why can't I send `None` as data in a POST request using Python's `requests` library?

Is there any disadvantage in using POST to retrieve data instead of GET in jQuery Ajax requests?

How to send Javascript/jQuery AJAX POST requests sequentially, looping over an array of request data, using Promises?

Using "open in new tab" fails to post data, debugging XHR requests with Chrome 'Developer Tools'