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...
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);
});
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);
});
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);
});
var request = require('request');
request.post('
{form:{ mes: "heydude" }},
function(error, response, body){
console.log(body);
});
Install request module, using
npm install requestIn 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) });
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...');
});