Post Data with Request Module on Node. Js

Post Data with Request Module on Node. Js

This module is 'request

I think i'm following every step but i'm missing an argument..

var request = require('request');
request.post({
        url: '
         body: "mes=heydude"
         }, function(error, response, body){
            console.log(body);
    });

on the other end i have

echo $_POST['mes'];

And i know the php isn't wrong...

2

8 Answers

EDIT: You should check out Needle. It does this for you and supports multipart data, and a lot more.

I figured out I was missing a header

var request = require('request');
request.post({
  headers: {'content-type' : 'application/x-www-form-urlencoded'},
  url:     '
  body:    "mes=heydude"
}, function(error, response, body){
  console.log(body);
});
1

When using request for an http POST you can add parameters this way:

var request = require('request');
request.post({
  url:     '
  form:    { mes: "heydude" }
}, function(error, response, body){
  console.log(body);
});

I had to post key value pairs without form and I could do it easily like below:

var request = require('request');

request({
  url: '
  method: 'POST',
  json: {mes: 'heydude'}
}, function(error, response, body){
  console.log(body);
});
0

If you're posting a json body, dont use the form parameter. Using form will make the arrays into field[0].attribute, field[1].attribute etc. Instead use body like so.

var jsonDataObj = {'mes': 'hey dude', 'yo': ['im here', 'and here']};
request.post({
    url: '
    body: jsonDataObj,
    json: true
  }, function(error, response, body){
  console.log(body);
});
3
var request = require('request');
request.post(' 
    {form:{ mes: "heydude" }}, 
    function(error, response, body){
        console.log(body);
});
  1. Install request module, using npm install request

  2. In code:

    var request = require('request');
    var data = '{ "request" : "msg", "data:" {"key1":' + Var1 + ', "key2":' + Var2 + '}}';
    var json_obj = JSON.parse(data);
    request.post({
        headers: {'content-type': 'application/json'},
        url: '
        form: json_obj
    }, function(error, response, body){
      console.log(body)
    });
    
1

I have to get the data from a POST method of the PHP code. What worked for me was:

const querystring = require('querystring');
const request = require('request');

const link = '
let params = { 'A': 'a', 'B': 'b' };

params = querystring.stringify(params); // changing into querystring eg 'A=a&B=b'

request.post({
  headers: { 'Content-Type': 'application/x-www-form-urlencoded' }, // important to interect with PHP
  url: link,
  body: params,
}, function(error, response, body){
  console.log(body);
});

I highly recommend axios install it with npm or yarn

const axios = require('axios');

axios.get(')
    .then( response => {
    console.log('Respuesta', response.data);
    })
    .catch( response => {
        console.log('Error', response);
    })
    .finally( () => {
        console.log('Finalmente...');
    });

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Robert Thorne
Author

Robert Thorne

Robert Thorne covers electric vehicle innovations, autonomous driving systems, global mobility trends, and automotive engineering developments.