Skip to content

Content-Type of type multipart must include a boundary parameter #1956

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
gpichot opened this issue Oct 22, 2024 · 2 comments
Closed

Content-Type of type multipart must include a boundary parameter #1956

gpichot opened this issue Oct 22, 2024 · 2 comments
Assignees
Labels
bug Something isn't working openapi-fetch Relevant to the openapi-fetch library

Comments

@gpichot
Copy link

gpichot commented Oct 22, 2024

Description

Hello,
First, thanks a lot for this great library, it's very useful in one of the projects I am working on.
I spent some to find the reason of the bug below so I thought to put it here for reference. Feel free to close the issue.
=> PR #1826 was a breaking change on our side as we migrated from 0.8.x to 0.12.x

POST requests with FormData were failing with Content-Type of type multipart must include a boundary parameter.

Reason is:

We had some code that was setting headers.set('Content-Type', 'multipart/form-data') (no idea why 🤔).
This header was removed in openapi-fetch prior to the PR. So the browser was setting the correct Content-Type.

After this PR, the header is now left in headers and therefore the browser does not set the correct Content-Type.
Hence the issue.
We fixed it by removing the line above.

Best! And again feel free to close this issue.

@gpichot gpichot added bug Something isn't working openapi-fetch Relevant to the openapi-fetch library labels Oct 22, 2024
@xGooddevilx
Copy link

xGooddevilx commented Feb 13, 2025

Hello, How are you doing 😊?

I ran into the same issue where setting 'Content-Type': 'multipart/form-data' in request headers caused the FormData payload to be sent as an empty object { }. Initially, I set the request header like this in both the middleware and the service:

const { data, error } = await client.POST('/api/media/upload', {
  body: file as any,
  headers: {
    'content-type': 'multipart/form-data',
  },
});

But the payload was empty. When I tried using native fetch in Next.js, everything worked fine, and the FormData was correctly sent with the full payload. I thought maybe I shouldn’t set the Content-Type manually, so I modified the service like this:

export const createMediaServices = (client: Client) => ({
  upload: async (file: File) => {
    try {
      const { data, error } = await client.POST('/api/media/upload', {
        body: file as any,
        bodySerializer: (body) => {
          const formData = new FormData();
          formData.set('file', body!.file!);
          console.log(formData.get('file'));
          return formData;
        },
      });
      return error ? R.Error(mapError(error)) : R.Ok(mappers.upload(data));
    } catch {
      return R.Error(mapError(undefined));
    }
  },
});

In the middleware, I’ve tried this approach:

export const requestMiddleware: Middleware['onRequest'] = async ({
  request,
  schemaPath,
}) => {
  const isMedia = schemaPath === '/api/media/upload';
  if (isMedia) {
    return undefined;
  }
  
  const contentType = request.method.toUpperCase() === 'PATCH'
    ? 'application/merge-patch+json'
    : 'application/ld+json';
  
  request.headers.set('content-type', contentType);
  return request;
};

However, it seems like openapi-fetch overrides the Content-Type and defaults to application/json, preventing the FormData from being processed correctly.

BTW I'm using version 0.10 of openapi-fetch

@gpichot

@gzm0
Copy link
Contributor

gzm0 commented Feb 19, 2025

TY very much for reporting this.

However, I'd propose to close this as wontfix:

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
Development

No branches or pull requests

4 participants