152

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...

Diego Torres
  • 5,308
  • 5
  • 25
  • 29
  • Why not use `http.request` directly? I have a feeling the body does not map to querystring parameters like that. Try `url: 'http://localhost/test2.php?mes=heydude'` – Raynos Jun 21 '11 at 22:25
  • In Node.js 18, the fetch API is available on the global scope by default https://stackoverflow.com/questions/6158933/how-is-an-http-post-request-made-in-node-js/71991867#71991867 – Abolfazl Roshanzamir Apr 24 '22 at 20:04

8 Answers8

218

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);
});
Reza Owliaei
  • 3,293
  • 7
  • 35
  • 55
Diego Torres
  • 5,308
  • 5
  • 25
  • 29
82

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);
});
TinyTimZamboni
  • 5,275
  • 3
  • 28
  • 24
52

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);
});
rupps
  • 9,712
  • 4
  • 55
  • 95
Ahsan
  • 3,845
  • 2
  • 36
  • 36
44

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);
});
Ricky Sahu
  • 23,455
  • 4
  • 42
  • 32
  • "jsonDataObj" must be stringified (JSON.stringify(...)) otherwise it crashes – Doctor Feb 03 '18 at 16:28
  • Actually mine crashes if you stringify it. It probably depends how the receiving server is set up. – Andrew Mar 23 '18 at 19:38
  • 1
    Thanks for your answer. Where can I find the doc for this? Since I doubted the post method, but the doc in Github is not enough fixing this issue. – Sky Apr 10 '18 at 09:39
16
var request = require('request');
request.post('http://localhost/test2.php', 
    {form:{ mes: "heydude" }}, 
    function(error, response, body){
        console.log(body);
});
aposto
  • 566
  • 5
  • 14
13
  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)
    });
    
Binary Brain
  • 1,170
  • 8
  • 20
Aniket B
  • 438
  • 1
  • 5
  • 14
  • Aside from the errors in the json string, this one did the trick for me! Note that the "form" section has an object passed to it, so you could just define everything in there as an object to begin with and not do that prep work – rgbflawed Jan 18 '17 at 21:59
4

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);
});
Rahmat Ali
  • 1,430
  • 2
  • 17
  • 29
-3

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...');
    });
Oscar Perez
  • 53
  • 1
  • 5