Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

send formData POST request with nodejs

I want to send a post request that contain a form data, i want to do that from nodejs to another external api, i don't have the front-end to send the formData, so all i have a javascript object that has keys and values, so how can i do that?, when i tried sending a normal object, i didn't get the right api response, but when i sent it with a POSTMAN client, i got the correct response.

enter image description here

like image 289
ProMaker Dev Avatar asked May 01 '26 04:05

ProMaker Dev


1 Answers

If you have a correct result in Postman, it's interesting to use the code generator in the same tools to have the desired code :). The button "</>" is on the right bar of the screen.

Here is the code generated from the tool :

var axios = require('axios');
var FormData = require('form-data');
var data = new FormData();
data.append('data', 'asldkfjalsdkjf');

var config = {
  method: 'post',
  url: 'https://some-domain.com/formdata',
  headers: { 
    ...data.getHeaders()
  },
  data : data
};

axios(config)
.then(function (response) {
  console.log(JSON.stringify(response.data));
})
.catch(function (error) {
  console.log(error);
});

It's cool, isn't it? One more thing, you have many options from NodeJS to C#, PHP.. :)

enter image description here

like image 186
Đăng Khoa Đinh Avatar answered May 03 '26 14:05

Đăng Khoa Đinh



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!