postmanlabs / postman-app-support

Postman is an API platform for building and using APIs. Postman simplifies each step of the API lifecycle and streamlines collaboration so you can create better APIs—faster.
https://www.postman.com
5.81k stars 839 forks source link

Sending empty form-data body does not add automatic content-type header #11983

Open JustusGreiberORGADATA opened 1 year ago

JustusGreiberORGADATA commented 1 year ago

Is there an existing issue for this?

Describe the Issue

If you select "form-data" for the body but uncheck all key-value pairs no Content-Type header is send to the API.

If you check one key-value pair Content-Type: multipart/form-data; ... is automatically set.

I expected the Content-Type header to be still set to multipart/form-data, if I wanted no automatic header, I would choose "none" for the body.

But maybe this is the desired behavior.

Steps To Reproduce

  1. Create a request.
  2. Add a body with type "form-data".
  3. Add key-value pairs
  4. Uncheck all key-value pairs
  5. Send the request and check the headers in the console. Content-Type is missing.

Screenshots or Videos

image

Operating System

Windows

Postman Version

10.13.0

Postman Platform

Postman App

User Account Type

Signed Out User

Additional Context?

No response

ppodgorskicrido commented 1 year ago

The same problem using binary option: "content-type" header exist in auto-generated headers, but it isn't send in request.

brnck commented 1 year ago

I'm having almost the same issue

When there is a form-data body and you're sending only file, Content-Type header is not being set.

Here's an example of what headers are being sent with only file added to the form:

image

And here's an example with dummy key added to the request:

image
ADTC commented 2 months ago

Unbelievable! This is still an issue. I had form-data with only one File key which is a bunch of files. Content-Type is missing in the header!

The workaround:

I just added another Text key called dummy and put a dummy value. Now the Content-Type is included in the header!

Dear Postman:

Year 2024, and this is STILL not actually fixed... Body type = form-data means ALWAYS include Content-Type: multipart/form-data; boundary=<calculated when request is sent>.

abdelhak002 commented 3 weeks ago

I am having the same issue: empty body when using form-data and the http method patch

@ADTC @brnck @JustusGreiberORGADATA @ppodgorskicrido

Edit (probably solved)

Issue Explanation:

This previous behavior occurs because HTML forms natively support only GET and POST methods. When you try to send a PATCH request using form-data in Postman or in JavaScript FormData(), some server-side frameworks or setups may not properly handle this combination, leading to an empty request body.

Potential Solution:

  1. Use the _method Parameter (Method Overriding):
    • You can work around this limitation by sending the request as a POST and using the _method parameter to indicate the actual method you want to use (PATCH, PUT, DELETE).
    • Example:
      • In the backend we have this endpoint: PATCH /user/update/{id}
      • From our client side we send a POST request to /user/update/{id}?_method=PATCH with the form-data body.
      • On the server side, the framework will treat this as a PATCH request.
   const formData = new FormData();
   formData.append('name', 'John Doe');
   formData.append('photo', fileInput.files[0]);

   fetch('/user/update/{id}?_method=PATCH', {
     method: 'POST',
     body: formData,
     headers: {
       'Accept': 'application/json',
     },
   })
   .then(response => response.json())
   .then(data => console.log('Success:', data))
   .catch(error => console.error('Error:', error));