There are lots of posts about how to handle request with multipart form data . But my use case is that I have a client that expects multipart form data response from server, and I need to write a simple nodejs server in order to test my client.
To write the simple server, I have the following:
var express = require('express');
var bodyParser = require('body-parser');
var FormData = require('form-data');
var app = express();
app.use(bodyParser.urlencoded({extended: true}));
app.use(bodyParser.json());
app.get('/describe', function(req, res) {
var form = new FormData();
form.append('part1', 'part 1 data');
form.append('part2', 'part 2 data');
res.setHeader('Content-Type', 'multipart/form-data');
res.send(form);
});
app.listen(3030, "0.0.0.0");
console.log('Listening on port 3030...');
Now when my client request localhost:3030/describe, the response header shows the following without the boundary value
Content-Type: multipart/form-data; charset=utf-8
And the content is downloaded as file instead of in the response body.
{"_overheadLength":208,"_valueLength":22,"_valuesToMeasure":[],"writable":false,"readable":true,"dataSize":0,"maxDataSize":2097152,"pauseStreams":true,"_released":false,"_streams":["----------------------------315683163006570790405079\r\nContent-Disposition: form-data; name=\"part1\"\r\n\r\n","part 1 data",null,"----------------------------315683163006570790405079\r\nContent-Disposition: form-data; name=\"part2\"\r\n\r\n","part 2 data",null],"_currentStream":null,"_boundary":"--------------------------315683163006570790405079"}
So my questions: 1. how do we make the boundary appears in the response header? 2. how do we make the form data response content show up in the response body instead as download file?
multipart/form-dataas part of a response cycle, but only as part of the request cycle. If you want the browser to understand the various fields in the body, you'll need to implement your own client-side parser for the response from the server, as this is a very unconventional API.