-
Notifications
You must be signed in to change notification settings - Fork 128
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
FormData support #37
Comments
Very interested in gaining access to the |
I did not know about const formData = new FormData()
formData.append('avatar', avatar)
formData.append('model', model)
formData.append('id', id)
$fetch('/avatar', {
method: 'POST',
body: formData,
headers: { 'Content-Disposition': formData }
}) |
Still a bug in nuxt 3 |
I think the problem comes from the content-length header. The error happens before sending the request:
Axios seems to be able to send the same exact request |
@posva We're seeing people encounter issues with undici - do you encounter the same issue if you downgrade y our node version? |
I get the same result with both node 18 and node 16 (latest patchs) |
when I set |
Can't send FormData. Any updates? |
The issue has been resolved after removing the |
As mentioned by @TusharRoy23, when using formData, do not specify a 'Content-Type' header, as it will automatically be set by fetch. |
I don't have one anymore but should be reproducible (if still there) when trying to send images, something like https://vueuse.org/core/useFileDialog/#usefiledialog. |
@Hebilicious I don't have. But If you still having this issue then you have to forcefully remove the Content-type from the header. |
any solution to this, having same issue , EDIT: my server had issue , its working fine |
I am having this issue. I am not setting the content type, but I am setting auth headers. It keeps sending the content-type as |
Meanwhile, Axios works great with this setup without any magic. Just create its instance in nuxt plugin |
This comment was marked as off-topic.
This comment was marked as off-topic.
This comment was marked as off-topic.
This comment was marked as off-topic.
This comment was marked as off-topic.
This comment was marked as off-topic.
I can confirm from the latest version:
|
Excellent. If anyone is still experiencing this, please let me know and I'll happily reopen. |
Hi, using nuxt 3, this doesn't work for me:
It also doesn't work if I don't specify { type: 'application/json' } for the file. Any ideas what I should do? My workaround at the moment is to use fetch instead of $fetch. |
@belfortf assuming you mean that the form body is not being sent, both JSON and form bodies work fine for me. The content information headers should be set automatically too. Could you provide some code of how you handle the incoming request? Or if the error is something else maybe provide the error? |
I have encountered a difference in the content type used by Specifically, |
@gokhantaskan $fetch is created with ofetch and should not differ (except perhaps in internal requests, which are powered by unenv) so I would be grateful of a reproduction and new issue if you are happy to make one. |
I noticed an issue with the Tested with |
I've identified the issue, and it's on my end: const $api = $fetch.create({
baseURL: config.public.apiPath,
credentials: "include",
// Convert request body to snake_case
onRequest({ options }) {
if (options.body) {
options.body = changeObjectKeysCase(options.body, "snake");
^^^ This line manipulates FormData and makes it plain object
}
},
... I solved it with the following code: onRequest({ options }) {
if (options.body) {
if (options.body instanceof FormData) {
const formData = new FormData();
for (const [key, value] of options.body.entries()) {
formData.append(snakeCase(key), value);
}
options.body = formData;
} else {
options.body = changeObjectKeysCase(options.body, "snake");
}
}
}, |
Seems a good idea for using
[object FormData]
to append proper content-type (adding in short)Originally posted by @pi0 in #36 (comment)
Other notes:
Content-Disposition
headerThe text was updated successfully, but these errors were encountered: