I am trying to form a web payload for a particular request body but unable to get it right. What I need is to pass my body data as below
data={'file-data':{"key1": "3","key2": "6","key3": "8"}}
My complete payload request looks like this
payload={url,headers, data={'file-data':{"key1": "3","key2": "6","key3": "8"}},files=files}
However, when I pass this, python tries to parse each individual key value and assigns to the 'file-data' key like this
file-data=key1file-data=key2file-data=key3
and so on for as many keys I pass within the nested dictionary. The requirement however, is to pass the entire dictionary as a literal content like this(without splitting the values by each key):
file-data={"key1": "3","key2": "6","key3": "8"}
The intended HTTP trace should thus ideally look like this:
POST /sample_URL/ HTTP/1.1Host: sample_host.comAuthorization: Basic XYZ=Cache-Control: no-cacheContent-Type: multipart/form-data; boundary=----UVWXXXX------WebKitFormBoundaryXYZContent-Disposition: form-data; name="file-data"{"key1": "3","key2": "6","key3":"8" }------WebKitFormBoundaryMANZXCContent-Disposition: form-data; name="file"; filename=""Content-Type: ------WebKitFormBoundaryBNM--
As such, I want to use this as part of a payload for a POST request(using python requests library). Any suggestions are appreciated in advance-
Edit1: To provide more clarity, the API definition is this:
BodyType: multipart/form-dataForm Parametersfile: required (file)The file to be uploadedfile-data: (string)Example:{"key1": "3","key2": "6","key3": "8"}
The python code snippet I used(after checking suggestions) is this:
#!/usr/bin/env python# -*- coding: utf-8 -*-import requestsurl = "https://sample_url/upload"filepath='mypath' filename='logo.png'f=open(filepath+'\\'+filename)filedata={'file-data':"{'key1': '3','key2': '6','key3': '8'}"} base64string = encodestring('%s:%s' % ('user', 'password').replace('\n', '')headers={'Content-type': 'multipart/form-data','Authorization':'Basic %s' % base64string} r = requests.post(url=url,headers=headers,data=filedata,files={'file':f})print r.text
The error I get now is still the same as shown below:
{"statusCode":400,"errorMessages":[{"severity":"ERROR","errorMessage":"An exception has occurred"]
It also says that some entries are either missing or incorrect. Note that I have tried passing the file parameter after opening it in binary mode as well but it throws the same error message
I got the HTTP trace printed out via python too and it looks like this:
send: 'POST sample_url HTTP/1.1Host: abc.comConnection: keep-aliveAccept-Encoding: gzip,deflateAccept: */*python-requests/2.11.1Content-type: multipart/form-dataAuthorization: Basic ABCDXXX=Content-Length: 342--CDXXXXYYYYYContent-Disposition:form-data; name="file-data"{\'key1\': \'3\',\'key2\': \'6\',\'key3\': \'8\'}--88cdLMNO999999Content-Disposition: form-data; name="file";filename="logo.png"\x89PNG\n\r\n--cbCDEXXXNNNN--