POST data with request module on Node.JS

node.jsRequest

node.js Problem Overview


This module is 'request https://github.com/mikeal/request

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

var request = require('request');
request.post({
		url: 'http://localhost/test2.php',
		 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...

node.js Solutions


Solution 1 - node.js

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:     'http://localhost/test2.php',
  body:    "mes=heydude"
}, function(error, response, body){
  console.log(body);
});

Solution 2 - node.js

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

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

Solution 3 - node.js

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

var request = require('request');

request({
  url: 'http://localhost/test2.php',
  method: 'POST',
  json: {mes: 'heydude'}
}, function(error, response, body){
  console.log(body);
});

Solution 4 - node.js

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: 'https://api.site.com',
    body: jsonDataObj,
    json: true
  }, function(error, response, body){
  console.log(body);
});

Solution 5 - node.js

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

Solution 6 - node.js

  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: 'http://localhost/PhpPage.php',
         form: json_obj
     }, function(error, response, body){
       console.log(body)
     });
    

Solution 7 - node.js

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 = 'http://your-website-link.com/sample.php';
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);
});

Solution 8 - node.js

I highly recommend axios https://www.npmjs.com/package/axios install it with npm or yarn

const axios = require('axios');

axios.get('http://your_server/your_script.php')
    .then( response => {
    console.log('Respuesta', response.data);
    })
    .catch( response => {
        console.log('Error', response);
    })
    .finally( () => {
        console.log('Finalmente...');
    });

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionDiego TorresView Question on Stackoverflow
Solution 1 - node.jsDiego TorresView Answer on Stackoverflow
Solution 2 - node.jsTinyTimZamboniView Answer on Stackoverflow
Solution 3 - node.jsRaptorView Answer on Stackoverflow
Solution 4 - node.jsRicky SahuView Answer on Stackoverflow
Solution 5 - node.jsapostoView Answer on Stackoverflow
Solution 6 - node.jsAniket BView Answer on Stackoverflow
Solution 7 - node.jsRahmat AliView Answer on Stackoverflow
Solution 8 - node.jsOscar PerezView Answer on Stackoverflow