Closed daviestar closed 6 years ago
bodyparser will only parse the json or form data, maybe you should try https://github.com/stream-utils/raw-body to get the raw request body.
Thankyou @dead-horse !
Here's some working code in case anyone else lands here:
import getRawBody from 'raw-body'
router.put('/image-upload', async (ctx) => {
const file = await getRawBody(ctx.req)
const bufferStream = new stream.PassThrough()
const writeStream = fs.createWriteStream(`${config.staticDir}/file.jpg`)
bufferStream.end(file)
bufferStream.pipe(writeStream)
ctx.body = {
status: 'uploaded!'
}
})
Apologies if this is the wrong place to ask this... but I haven't been able to get anywhere with it and would be very grateful if a knowledgeable person could point me in the right direction...
I currently upload images direct to Amazon S3 like so:
and I'd like to create an endpoint in Koa to replicate this type of file upload for development and tests. When I try, the body is always an empty object:
I can see from Chrome DevTools that the request body is binary data.
content-type
isimage/jpeg
.Any advice greatly appreciated!