POST data with Node.JS request module

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

I think I follow every step, but I am 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

 echo $_POST['mes']; 

And I know php is not wrong ...

+125
request
Jun 21 '11 at 10:17
source share
8 answers

EDIT: You have to check out Needle . It does this for you and supports multi-page data and much more.

I realized that I am missing the title

 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); }); 
+181
Jun 21 2018-11-21T00:
source share

When using request for http POST, you can add parameters as follows:

 var request = require('request'); request.post({ url: 'http://localhost/test2.php', form: { mes: "heydude" } }, function(error, response, body){ console.log(body); }); 
+76
Oct. 31 '13 at 18:00
source share

I had to publish key-value pairs without a form, and I could easily do this, as shown below:

 var request = require('request'); request({ url: 'http://localhost/test2.php', method: 'POST', json: {mes: 'heydude'} }, function(error, response, body){ console.log(body); }); 
+37
Aug 17 '16 at 6:38
source share

If you are submitting a json body, do not use the form parameter. Using form will make arrays in field[0].attribute , field[1].attribute , etc. Use body instead.

 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); }); 
+34
May 31 '17 at 17:34
source share
 var request = require('request'); request.post('http://localhost/test2.php', {form:{ mes: "heydude" }}, function(error, response, body){ console.log(body); }); 
+16
Nov 04 '13 at 9:29
source share
  1. Install the 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) }); 
+11
Nov 22 '16 at 7:08
source share

I have to get the data from the 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); }); 
0
Aug 31 '19 at 18:10
source share

I highly recommend axios https://www.npmjs.com/package/axios to install it using 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...'); }); 
-one
Jul 20 '19 at 5:13
source share



All Articles