Post using multipart/form-data with bent in node.js

Viewed 312

I am trying to use bent to POST in node.js and I have been able to post application/json with below code and received the data at the other end.

const bent = require('bent');
const postJSON = bent('POST', 'json', { 'Content-Type': 'application/json'});
const field = 'data';

return await postJSON('http://localhost/endpoint', { field });

However when I do multipart/form-data with the below code, no error occurred but I didn't receive any data at the other end.

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);
1 Answers

Figured the issue out, correct form header was not passed to bent so I had to rewrite as below

return await post('http://localhost/endpoint', form, form.getHeaders());
Related