Sending formData to /server with files and parsing body
Hi there,
I'm tying to send a file from browser to /server
/api so that I can inject some header information server side and proxy that request to an external API, however the request always gets corrupted in the process. I can hit the external api fine so I know it's somewhere in the defineEventHandler() function. I have a feeling that it might be to do with the readBody() method but I've tried both readRawBody() and readMultipartFormData() and neither work. Basically I just want to pass the formData from the Request straight into another $fetch within the server route without changing the data.
Hope that makes sense. Thanks!
2 Replies
Unknown User•2y ago
Message Not Public
Sign In & Join Server To View
Have you tried to send the image data as base64 to the server? Then, you could convert the base64 into a buffer and store it somewhere on the server.
To convert the base64 image data to a buffer, I have used this package https://www.npmjs.com/package/data-uri-to-buffer
npm
data-uri-to-buffer
Generate a Buffer instance from a Data URI string. Latest version: 5.0.1, last published: 19 days ago. Start using data-uri-to-buffer in your project by running
npm i data-uri-to-buffer
. There are 272 other projects in the npm registry using data-uri-to-buffer.