简体   繁体   中英

Sending JSON and File in same multipart/form-data POST request

I have a form with an image and fields that I want to submit.

I'm running into problems since my POST request has content-type multipart/form-data and not JSON. For example, any null values in my form are converted to "null" string...

Is it a bad practice ton send JSON and files in the same request ?

Perhaps I should decompose my POST in 2 seperate ones ?

One for the file in multipart/form-data

One for the JSON in application/json

To Expand @Mikkel's answer - you can write the following code to parse the JSON sent as string:

var multipart = require('connect-multiparty');
var multipartMiddleware = multipart();
app.post('/multipart', multipartMiddleware, function(req, res) {
    console.log('multipart API was called.');
    console.log(req.body);
    var bodyAsJson = JSON.parse(req.body.data);
    console.log(bodyAsJson);
    // some other multipart code...
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM