Creating a Nodejs server that send response with multipart form data

前提是你 提交于 2020-12-30 03:16:29

问题


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?


回答1:


To send your form data you'll want to pipe it (see documentation), like this:

form.pipe(res);

To add the boundary into the header, you can do something like this:

res.setHeader('Content-Type', 'multipart/form-data; boundary='+form.getBoundary());

Now, about the "save as" box: The browser uses content-type to figure out what to do with the file. So if you want it to be displayed in the browser window, a good choice would be text/plain (or possibly text/html if that doesn't work). Then you could put your multipart/form-data into something like x-content-type.

(I'm assuming eventually you'll be using XHR or fetch, and at that point you can switch the content-type back. the text/plain thing is just a temporary solution to get the data to show up in a standard web browser for testing.)

Putting it all together:

app.get('/describe', function(req, res) {
  var form = new FormData();
  form.append('part1', 'part 1 data');
  form.append('part2', 'part 2 data');
  res.setHeader('x-Content-Type', 'multipart/form-data; boundary='+form._boundary);
  res.setHeader('Content-Type', 'text/plain');
  form.pipe(res);
});



回答2:


In my case, it was POST request that was being called by a webhook and I used multer did the magic too. Here is the code.

const express = require('express');
const bodyParser = require('body-parser');
var multer = require('multer');
var upload = multer();
const app = express();
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));

app.post('/', upload.none(), function (req, res) {
  console.log("Data",req.body) 
  res.status(200).send('OK');
});

app.listen(port);


来源:https://stackoverflow.com/questions/50880939/creating-a-nodejs-server-that-send-response-with-multipart-form-data

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!