Sending request with file and text

Describe the bug
I would like to send a request that contains both a file and some meta data about the file.

However, when I create the request, I canā€™t set the file in the x-www-form-urlencoded so when I send it, it does not have file information.

When I create the request using form-data, it contains the file information, but my server side API receives an empty body

To Reproduce
Steps to reproduce the behavior:

  1. create a new request
  2. set body to form-data
  3. type in ā€˜imageā€™ for key and select a file for value
  4. type in ā€˜affiliationā€™ for key and type in affiliation value
  5. type in ā€˜nameā€™ for key and type in ā€˜testā€™ for value
  6. select ā€œPostā€
  7. type in the URL
  8. click the send button

repeat steps, but select x-www-form-urlencoded

Expected behavior
I expect the image data and the other data to come through in my api, but it doesnā€™t

Screenshots
Here is the result using form-data. NOTE: my back end API has failed validation as it cannot find ā€˜affiliationā€™, ā€˜imageā€™ or ā€˜nameā€™ in the request (even though itā€™s present in the form-data)

Here is the result using www-form-urlencoded. NOTE: the back end has created the entry in the DB, but it is missing the ā€˜imageā€™ portion of the request (itā€™s not present in the request)

App information (please complete the following information):

  • App Type : REST API
  • Postman Version - latest version
  • OS: windows 7

Additional context
I have searched the web for a way to do this and it seem like there isnā€™t a good answer

Can you double check by opening the Postman Console that Postman is indeed not sending the other form-data fields as your API complains?

IF it were sending the other form-data fieldsā€¦ then the code would execute correctlyā€¦ right?
as the only data that I am setting is the relevant data for the API being tested.

@vdespa I met the same situation. I donā€™t know why postman sent a null object as the body in my case if I was using form-data mode trying to send text data and files data together. If i use the x-www-form-urlencoded to send the text data, it will be ok to send the key-value pairs with only problem is not able to send files together. What is the trick to actually to use form-data mode in current version? I remember few month ago when I used postman, I was able to send files with text together.

I also came across this requirementā€¦i.e. to send file (image) along with some text parameters in x-www-form-urlencodedā€¦
There is no option to select the type of parameter (file or text) as its available in form-dataā€¦
If I send this as form-data with a header as ā€œContent-Type application/x-www-urlencodedā€, then the image does not get sent successfullyā€¦
Any idea how can this be overcome? I am kind of blocked

Maybe I am wrong, but AFAIK x-www-form-urlencoded is not suitable for data (as the information will be URL encoded).

You should use multipart/form-data instead. Have you tried that?

Thanks @vdespaā€¦
The multipart/form-data does work

So I am pretty sure you are ā€œkind of out of luckā€ on this. Here is the ā€œissueā€ for this going back more than 5 years ā€¦ up shot, there is a work around if you turn your content into files. Otherwise, you will have to wait for Postman to catchup to curl :stuck_out_tongue:

https://github.com/postmanlabs/postman-app-support/issues/8203

I can confirm this issue is still live and has not been fixed. I presume because it has been poorly explained.

I use a middle-man API server to automate doing remote actions while Iā€™m testing, saves a lot of micro.

I was writing an API to upload files to test cases, when I stumbled across this problem.

Using the multipart/form-data, Iā€™ve specified several parameters that the middle-man API reads and takes action on. This includes the payload for a application/octet-stream file.

However Postman is not sending the key or value for the file. See below as to what has been received

ImmutableMultiDict([('File-Name', 'test.py'), ('object_type', 'test-cases'), ('Content-Type', 'application/octet-stream'), ('projectID', '12345'), ('test_case_id', '00001')])

This is the representation in cURL that Postman provided

curl --location --request POST 'localhost:7810/api/v1/resources/upload_to_test_case' \
--header 'Authorization: Bearer <secret>' \
--form 'File-Name="test.py"' \
--form 'object_type="test-cases"' \
--form 'Content-Type="application/octet-stream"' \
--form 'contents=@"/home/<user>/Downloads/test.py"' \
--form 'projectID="12345"' \
--form 'test_case_id="000001"'

However when I add a EOL character after contents, the payload of the file and key shows up.

ImmutableMultiDict([('File-Name', 'test.py'), ('object_type', 'test-cases'), ('Content-Type', 'application/octet-stream'), ('"contents', '<omitted>'), ('projectID', '12345'), ('test_case_id', '000001')])

So, if you are stuck in the same boat as me, itā€™s incredibly hacky but you can do it if you add that EOL character and just expect to do some formatting at the other side.

Just set all the data in form-data and set the image, then write you method in controller for upload the photo first, and you can see the data is allowed to the request.body.