I have an API which requires a multipart/form-data content to be send in the body in order to upload a file(image) using POST. I have chosen to use python's requests module as it's easy for newbies like me.There are two form parameters to pass; one is called file, which should be the actual file data and the other is called file-data, which is a dictionary object as below:
{"param1": param_val,"param2":param_val1}
I have seen http://docs.python-requests.org/en/master/user/quickstart/#post-a-multipart-encoded-file but it doesn't speak about passing form parameters, i.e as if these parameters were values being passed from within a web form which provides placeholders for the file name and the other parameters. Note that if I pass ordinary parameters to the API call like:
r = requests.post(url, headers= headers,params=params)
I would end up sending the parameters after the URL as in URL?param1=param_val>...etc which doesn't work in this case. So I basically need a way to send the form parameters mentioned above as form data of the request. If I could get some guidance around the syntax of the POST call to make in the scenario it would help a lot-
****edited for more clarity****
The desired HTTP trace of my POST request is as shown below:
POST <application URL>
Host: <Host server name>
Authorization: Basic ABCDXXXX
Cache-Control: no-cache
Postman-Token: XXXXXX-ABACC-XXXXX
Content-Type: multipart/form-data; boundary=----
------WebKitFormBoundaryXYXXXXXXXXXXXXXXXXX
Content-Disposition: form-data; name="file-data"
{"description": "Sample desc","languageCode": "en" }
------WebKitFormBoundaryABCXXXXXXXXXXXXX
Content-Disposition: form-data; name="file"; filename=""
Content-Type:
------WebKitFormBoundaryWWWWXXXXXXXXXXXX--
If I use the form:
r = requests.post(url,headers=headers,data=filedata,files=files)
I end up getting an error message '400 199-repeated entities[]' which on closer investigation reveals that the request is trying to unpack the tuples in my file-data variable and ends up creating two content boundaries, one for the description and other for the languagecode. Is there a way I can just make it pass file-data in the form parameters just once as shown above?
****end of edit*****
Thanks in advance- Regards