Post 使用 multipart/form-data 并在 node.js 中弯曲
Post using multipart/form-data with bent in node.js
我正在尝试在 node.js 中使用 bent
到 POST
,并且我已经能够使用以下代码 post application/json
并接收到数据在另一端。
const bent = require('bent');
const postJSON = bent('POST', 'json', { 'Content-Type': 'application/json'});
const field = 'data';
return await postJSON('http://localhost/endpoint', { field });
然而,当我使用下面的代码multipart/form-data
时,没有发生错误,但我在另一端没有收到任何数据。
var FormData = require('form-data');
var form = new FormData();
form.append('my_field', 'my value');
form.append('my_buffer', 'my data');
const bent = require('bent');
//const postJSON = bent('POST', 'string', { 'Content-Type': 'multipart/form-data'});
const postJSON = bent('POST', 'buffer', { 'Content-Type': 'multipart/form-data'});
return await postJSON('http://localhost/endpoint', form);
解决了问题,正确的形式 header 没有传递给 bent 所以我不得不重写如下
return await post('http://localhost/endpoint', form, form.getHeaders());
我正在尝试在 node.js 中使用 bent
到 POST
,并且我已经能够使用以下代码 post application/json
并接收到数据在另一端。
const bent = require('bent');
const postJSON = bent('POST', 'json', { 'Content-Type': 'application/json'});
const field = 'data';
return await postJSON('http://localhost/endpoint', { field });
然而,当我使用下面的代码multipart/form-data
时,没有发生错误,但我在另一端没有收到任何数据。
var FormData = require('form-data');
var form = new FormData();
form.append('my_field', 'my value');
form.append('my_buffer', 'my data');
const bent = require('bent');
//const postJSON = bent('POST', 'string', { 'Content-Type': 'multipart/form-data'});
const postJSON = bent('POST', 'buffer', { 'Content-Type': 'multipart/form-data'});
return await postJSON('http://localhost/endpoint', form);
解决了问题,正确的形式 header 没有传递给 bent 所以我不得不重写如下
return await post('http://localhost/endpoint', form, form.getHeaders());