fix(openapi-fetch): fix headers for FormData #1192
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
We need to delete the
Content-Type
header if the serialized body is aFormData
. This allows the browser to set theContent-Type
and boundary expression correctly.Resolves #1191
Changes
Because
FormData
gets special handling by the browser, we need to give it special handling too.Check if the serialized body is an instance of
FormData
, and if so, delete theContent-Type
header.Resolves #1191
How to Review
I've tested this in my production app but I think you'd need to whip up a simple test project to see for yourself. I'm out of time for that today though, sorry!
One hiccup in the tests -
vitest-fetch-mock
does not add the form field boundary like the browser does. Ideally we could confirm that theContent-Type
forFormData
bodies are correct:Instead, we must check if it is null:
edit: Oh, maybe it's just that the browser adds the boundary as the request is processed, and testing for
null
is correct@nilsso Please give try this out.
Checklist
[ ]examples/
directory updated (only applicable for openapi-typescript)