Skip to content

Request Header 'Content-Type' no longer set for requests with body serialized as FormData #1548

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
1 task done
schuch opened this issue Feb 16, 2024 · 3 comments
Closed
1 task done
Labels
bug Something isn't working openapi-fetch Relevant to the openapi-fetch library

Comments

@schuch
Copy link

schuch commented Feb 16, 2024

Description

After upgrading openapi-fetch from 0.8.2 to 0.9.1 we see 415 Unsupported Media Type responses for multipart/form-data requests.

We have investigated how the request has changed as a result of the update. We noticed that the request header Content-Type: multipart/form-data; boundary=... is no longer sent with the request.

A copy from of the request (as cUrl) from the developer console:

curl 'https://...' \
  -H 'Accept: */*' \
  --data-raw $'------WebKitFormBoundary9Y0HMzSGLEhBsYQT\r\nContent-Disposition: form-data; name="file"; filename="test.pdf"\r\nContent-Type: application/pdf\r\n\r\n\r\n------WebKitFormBoundary9Y0HMzSGLEhBsYQT--\r\n' \
  --compressed

(omitted a few headers for better clarity)

Reproduction

We use the following code to perform the request:

await POST(`/...`, {
    params: {
      ...
    },
    body: {
      file
    },
    bodySerializer: (body) => {
      const formData = new FormData();
      for (const name in body) {
        formData.append(name, body[name as keyof typeof body]);
      }
      return formData;
    }
).then(...)

Expected result

openapi-fetch create a proper multipart form-data request, e.g.:

curl 'https://...' \
  -H 'Accept: */*' \
  -H 'Content-Type: multipart/form-data; boundary=----WebKitFormBoundaryy81TvwmCQwmGHDDw' \
  --data-raw $'------WebKitFormBoundaryy81TvwmCQwmGHDDw\r\nContent-Disposition: form-data; name="file"; filename="test.pdf"\r\nContent-Type: application/pdf\r\n\r\n\r\n------WebKitFormBoundaryy81TvwmCQwmGHDDw--\r\n' \
  --compressed

(copied as cUrl from a successful request from the developer console, omitted a few headers for better clarity)

Checklist

@schuch schuch added bug Something isn't working openapi-fetch Relevant to the openapi-fetch library labels Feb 16, 2024
@schuch schuch changed the title Request Header 'Content-Type' no longer set for body serialized as FormData Request Header 'Content-Type' no longer set for requests with body serialized as FormData Feb 16, 2024
@shirish87
Copy link
Contributor

I have the same issue. Inspecting the Request object in openapi-fetch shows the content-type header that used to be set in 0.8.2 multipart/form-data; boundary=----formdata-undici-0.6228903411172271 is no longer set in 0.9.x

shirish87 added a commit to shirish87/openapi-typescript that referenced this issue Feb 17, 2024
…t. call. Fixes openapi-ts#1548

content-type header with multipart boundary set by Request() is retained
@lukasbash
Copy link

Is there a chance to get this in a patch release as soon as merged? Would be awesome!

@drwpow drwpow closed this as completed in a5a9cc7 Feb 19, 2024
@drwpow
Copy link
Contributor

drwpow commented Feb 19, 2024

Released in 0.9.2!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working openapi-fetch Relevant to the openapi-fetch library
Projects
None yet
Development

No branches or pull requests

4 participants