-
Notifications
You must be signed in to change notification settings - Fork 763
Description
Q&A (please complete the following information)
- OS: macOS
- Environment: Chrome 80, Node.js v12.9.1
- Method of installation: npm
- Swagger-Client version: 3.9.5, 3.10.0
- Swagger/OpenAPI version: OpenAPI 3.0.2
Content & configuration
Swagger/OpenAPI definition:
openapi: 3.0.2
info:
title: test
version: 1.0.0
paths:
"/foo":
post:
operationId: foo
requestBody:
content:
multipart/form-data:
schema:
type: object
properties:
a:
type: number
b:
type: number
responses:
'200':
description: bar
Swagger-Client usage:
There is no some specific configuration, just:
new SwaggerClient({ spec })
Describe the bug you're encountering
swagger-client generates an invalid value "multipart/form-data" for the header "Content-Type" when there is a request with FormData as the body. The parameter "boundary" is missed in the header value.
To reproduce...
- Clone a repository with an example:
git clone https://github.com/nikolaas/swagger-client-multipart-bug.git
- Install dependencies:
cd swagger-client-multipart-bug
npm install
- Run the example:
node index.js
- See the output:
{
url: '/foo',
credentials: 'same-origin',
headers: { 'Content-Type': 'multipart/form-data' }, // <-- the invalid header
method: 'POST',
body: FormData {
// FormData content here
}
}
Expected behavior
The expected value of the header is "multipart/form-data; boundary=some-boundary".
Additional context or thoughts
By RFC 2046 the header "Content-Type" for multipart requests must contain parameter "boundary":
The Content-Type field for multipart entities requires one parameter, "boundary".
In my opinion, the bug affects the basic functionality. I bumped with it when I tried to send the first multipart request. I don't know why nobody reported it before.
I'm confused. Maybe I don't understand something. Can you tell me this behavior is a bug? Or maybe am I doing something wrong?
Workaround
To workaround the bug you can override the HTTP client. The overridden HTTP client must remove the invalid header "Content-Type". After that, the browser will generate a valid value of the header based on the request body:
const http = (req) => fetch(fixMultipartContentType(req));
const swagger = new Swagger({ spec, http });
function fixMultipartContentType(req) {
if (req.headers == undefined) {
return req;
}
if (req.headers['Content-Type'] !== 'multipart/form-data') {
return req;
}
if (!(req.body instanceof FormData)) {
return req;
}
const headers = { ...req.headers };
delete headers['Content-Type'];
return { ...req, headers };
}