Skip to content
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

Wretch loses the request body for the PUT, POST, and PATCH methods if it is FormData #231

Closed
am1rb opened this issue May 21, 2024 · 0 comments · Fixed by #232
Closed

Wretch loses the request body for the PUT, POST, and PATCH methods if it is FormData #231

am1rb opened this issue May 21, 2024 · 0 comments · Fixed by #232
Labels

Comments

@am1rb
Copy link
Contributor

am1rb commented May 21, 2024

Hello there. I realized that wretch loses the request body for the PUT, POST, and PATCH methods if the body type is FormData and you pass it directly to the .put(), .post(), or .patch() methods as their first argument.

A sample code to demo the problem:

const requestBody = new FormData()
requestBody.append("field1", "data 1");
 requestBody.append("field2", "data 2");

wretch()
  // here the body is passed as the first argument, and the method does Jasonify it before attaching the data to the request.
  .post(requestBody, "SERVER_URL")

To see the problem in action, please check this sandbox out, and click on the red button. As you see, the Content-Type value is wrong, and there is nothing in the parsedBody.

We know that the native fetch API supports FormData, and I think that, since wretch is supposed to be a wrapper around it, the .put(), .post(), and .patch() methods need to support FormData inputs.

To address the issue, we need to update this line to exclude FormData objects:
https://github.com/elbywan/wretch/blob/master/src/core.ts#L95
Maybe something like:

const jsonify = (body instanceof FormData)===false && typeof body === "object" && (!base._options.headers || !contentType || isLikelyJsonMime(contentType))
am1rb added a commit to am1rb/wretch that referenced this issue May 23, 2024
am1rb added a commit to am1rb/wretch that referenced this issue May 23, 2024
@elbywan elbywan added the bug label Jun 1, 2024
@elbywan elbywan closed this as completed in 4693837 Jun 1, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants