question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

cannot send FormData in `$fetch` body

See original GitHub issue

Environment

Operating System: Windows 10 Node Version: v14.16.1 Nuxt Version: 3.0.0-rc.08 Builder: vite Runtime Modules: - Build Modules: -

Reproduction

  • Create a new nuxt3 project ( doesn’t matter if you are using rc10 )
  • Create a new simple component to upload an image
  • Put in the component an input type file like as follows : <input type="file" id="file-input" multiple @change="onInputChange" />
  • Add a script tag and create a new method onInputChange like this : onInputChange (e) { const file = e.target.files await $fetch('/api/uploadFile', { method: "POST", body: { files: file }, headers : { "Content-Type": "multipart/form-data" } }) }
  • After that create a new ssr api in the server/api directory like this :

import FormData from “form-data”; import { manageErrorServer } from “~~/utils/manageErrorServer”; export default defineEventHandler(async (event) => { const config = useRuntimeConfig(); const body = await useBody(event); try{ const res: Record<string, any> = await $fetch( ${config.API_BASE_URL}+“smart-product”,{ method: ‘POST’, body: body, headers: { “Content-Type”: “mutlipart/form-data” } } ) return res } catch (e) { console.log("erreur " + e.response) manageErrorServer(e); } });

Describe the bug

The multipart/form-data is not sent by $fetch, either on the first call to the ssr api or on the second call to the backend

Additional context

No response

Logs

No response

Issue Analytics

  • State:open
  • Created a year ago
  • Comments:6 (2 by maintainers)

github_iconTop GitHub Comments

2reactions
tak-hocommented, Dec 8, 2022

Me too, its not working for me, I raise another issue about posting file fail using FormData with ofetch https://github.com/unjs/ofetch/issues/186

0reactions
lenimbuguacommented, Dec 12, 2022

Similar issue, if anyone knows a workaround without needing to install axios please.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Why is the FormData object not being sent via fetch()?
For some reason it's not working. The FormData is not getting send. It seems that the data from the form is actually stored...
Read more >
Fix uploading files using fetch and multipart/form-data
I had a problem. The files data was not actually sent to the server. The solution was this: you must NOT set the...
Read more >
How to use fetch to POST form data as JSON to your API
Step 3. Format the data as JSON and POST it to a URL with fetch ; postFormDataAsJson( ; }) ; /** * We...
Read more >
Using the Fetch API - MDN Web Docs
The Fetch API provides a JavaScript interface for accessing and manipulating parts of the protocol, such as requests and responses.
Read more >
POST Form Data as JSON with Fetch API in JavaScript
Use JavaScript to convert HTML form data to JSON & submit to server... Professional JavaScript Course: ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found