-
-
Notifications
You must be signed in to change notification settings - Fork 532
Automatically encode the body when "Accept": "application/x-www-form-encoded"
header is passed
#2069
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
Comments
Thanks for suggesting. I’m not opposed to adding an additional check here, if that would satisfy this. Just to clarify, are you proposing something like the following? export function defaultBodySerializer(body) {
- if (body instanceof FormData) {
+ if (body instanceof FormData || body['x-www-form-encoded']) {
return body;
} Even though this is technically a nonstandard parameter, this does seem common enough where it’s cheap to support so I’d be open to it. I think we could also append to the docs explaining this as well. Does that match your thinking? |
I think we cannot get the
|
Ah that’s doable. Wasn’t sure if it was a header param or something passed in the request body. Would accept a PR for this with tests! If additional work is needed we may have to discuss tradeoffs, but right now this seems “free” both in runtime and code size so no reason not to! |
I will try to open a PR this week. Our use case for this in Openverse is sending the token request to a Django Rest Framework API with the following schema: Ideally, I would prefer to send the request like the one below, and have
But I don't think the |
It doesn’t; that’s the tradeoff with openapi-fetch. In order to have the actual OpenAPI schema in runtime, you’d need to pay a cost of heavy client weight and memory usage. But there’s almost always a way to get TypeScript to throw an error that tells the user they need to pass in X, Y, and Z settings that lead to the result you want |
Problem
For the endpoints requiring
x-www-form-encoded
body parameters, the request body is serialized as a string, and then sent as an empty object string{}
. The solution is to provide a custom body parser that converts the plain object of the body into URL-encoded string. This is not documented, and is difficult to debug.Possible solutions
The simplest solution would be to document the need to pass a custom
bodySerializer
.A better solution would be to automatically encode the body if the header is passed.
The default
defaultBodySerializer
could accept thefetchOptions.headers
parameter, and returnnew URLSearchParams(body).toString
if the"Accept": "x-www-form-encoded"
header is passed by the user.openapi-typescript/packages/openapi-fetch/src/index.js
Lines 564 to 573 in d4689b1
Additional context
I ran into this problem when working on
@openverse/api-client
. Openverse API uses Django OAuth Toolkit for authentication, and the token requests require the body to be url-encoded. The API requests from theapi-client
were failing due toinvalid_grant_type
. Turned out that the request was sending an empty body{}
.The text was updated successfully, but these errors were encountered: