Skip to content Skip to sidebar Skip to footer

Send An Xml File To Rest Api Using Python Requests

I am trying to send an XML file with a rest api POST. the api takes the xml and creates a new entitiy. I am trying to open the file and then sending it through requests. filename =

Solution 1:

Your API takes XML, not JSON. When you say, data = json.dumps(...), you are passing JSON to your API. This is the reason for your first error message -- 503 (API not able to get the right input).

requests.post() takes ether a dictionary, a string, or a file-like object as its data= parameter. When you do data = foo.readlines(), you are passing in a list (which is neither a string nor a dictionary. This is the reason for your second error message -- "ValueError: too many values to unpack".

Without knowing your API, it is hard to guess what is correct. Having said that, try this:

filename = 'test.xml'response = requests.post(api_url, data=open(filename).read())

Or, nearly equivalently, this:

filename = 'test.xml'response = requests.post(api_url, data=open(filename))

Solution 2:

I think the problem is, you are trying to convert the xml file into json data (using json.dumps()) directly and post it. I guess the API accepts JSON. Then first try to convert the xml file to a python dictionary / whatever data structure suits it. Then convert it into json.

Post a Comment for "Send An Xml File To Rest Api Using Python Requests"