i want to pass data with GET method in node js , my response is ok but in php file $_GET is empty. what is wrong?
var querystring = require('querystring');
var req = require('request');
var form = {
number: 'test',
msg: 'test',
};
var formData = querystring.stringify(form);
var contentLength = formData.length;
req({
headers: {
'Content-Length': contentLength,
'Content-Type': 'application/x-www-form-urlencoded'
},
uri: 'http://localhost:8080/sms/index.php',
body: formData,
method: 'GET'
}, function (err, res, body) {
console.log('res is',res);
console.log('err',err);
});
An HTTP GET request cannot have a request body. $_GET parses parameters from the query string. The headers Content-Lengthand Content-Type do not make sense for a GET request as they apply to the body of the request, which GET cannot have. You need to use the qs option instead of body.
req({
uri: 'http://localhost:8080/sms/index.php',
qs: form,
method: 'GET'
}, function (err, res, body) {
//
});
Related
I am trying to make a post request on url https://test.cashfree.com/api/v2/subscription-plans using node js requests but getting this body in return:
{"status":"ERROR","subCode":"400","message":"Invalid JSON in request body"}
This is my Code:
var querystring = require('querystring');
var request = require('request');
var form = {
planId: "NJGRKON12354",
planName: "Rent Product",
type: "PERIODIC",
amount: 100,
intervalType: "month",
intervals: 1
};
var formData = querystring.stringify(form);
var contentLength = formData.length;
request({
headers: {
'X-Client-Id': 'XXXXX',
'X-Client-Secret': 'XXXXXX',
'Content-Type': 'application/json'
},
uri: 'https://test.cashfree.com/api/v2/subscription-plans',
body: formData,
method: 'POST'
}, function (error1, res1, body) {
console.log('statusCode:', res1.statusCode);
console.log("Body: ", body);
});
When i try with same headers and body in postman its Running.
Postman Hit
Here is a basic code:
const superagent = require('superagent');
superagent.get('https://api.arabam.com/pp/step')
.query({ apikey: '_V85Kref7xGZHc1XRpUmOhDDd07zhZTOvUSIbJe_sSNHSDV79EjODA==' })
.end((err, res) => {
if (err) { return console.log(err); }
console.log(res.body.url);
console.log(res.body.explanation);
});
But apikey is header rather than query. How do I send it as a header?
Edit:
I have tried using request module and it simply says that I'm unable to access it
var request = require("request");
request({
uri: "https://api.arabam.com/pp/step",
method: "GET",
'Content-Type' : "application/json",
apikey: "_V85Kref7xGZHc1XRpUmOhDDd07zhZTOvUSIbJe_sSNHSDV79EjODA=="
}, function(error, response, body) {
console.log(body);
});
It says unauthorized to access
From the Setting header fields doc, you should use
request.set('apikey', '_V85Kref7xGZHc1XRpUmOhDDd07zhZTOvUSIbJe_sSNHSDV79EjODA==')
to set the header field.
I am trying to make a get request using the NPM Request module and am having trouble passing in a params argument.
Going through the docs I can't tell what the correct syntax is.
makeRequest(req, res, num, cookie) {
request({
headers: {
'Cookie': cookie
},
url: 'https://api.domain.com/path',
params: num // this is incorrect
},
(error, response, body) => {
res.json({
msg: "Success"
})
}
})
}
How can I pass a params argument into a request?
https://www.npmjs.com/package/request
qs - object containing querystring values to be appended to the uri
request({
headers: {
'Cookie': cookie
},
url: 'https://api.domain.com/path',
qs: { num: 1}
})
This should create a url
https://api.domain.com/path?num=1
I am creating a project using Node.js. I want to call my requests in parallel. For achieving this, I have installed the async module. Here is my code:
var requests = [{
url:url,
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': 'Bearer ' + req.cookies.apitoken
},
json: finalArr,
}];
async.map(requests, function(obj, callback) {
// Iterator function
request(obj, function(error, response, body) {
if (!error && response.statusCode == 200) {
// Transform data here or pass it on
var body = JSON.parse(body);
callback(null, body);
}
else {
var body = JSON.stringify(body);
console.log(body)
callback(error || response.statusCode);
}
});
})
I got undefined every time in console.log(body). When I am using GET requests using this module for other requests then everything works fine.
It looks like you're using the request module, but didn't tag it as such.
If I'm right, then your options object is incorrect, from the documentation there's not a data key that's respected. Instead, you should use body or formData or json depending on what kind of data you're pushing up. From your headers, I'd go with json.
var requests = [{
url:url,
method: 'POST',
headers: { // Content-Type header automatically set if json is true
'Authorization': 'Bearer ' + req.cookies.apitoken
},
json: finalArr,
}];
I searched here and there and ended up with no finding regarding putAsync method of promisified request by bluebird.
var request = require('request');
var Promise = require('bluebird');
Promise.promisifyAll(require("request"));
request.putAsync({
uri: buApiUrl,
headers: {
'content-type': 'application/json'
},
body: JSON.stringify({
name: BU,
workstations: formattedWorkStaions[BU]
})
}).spread(function (response, body) {
debugHelper.log(body);
}).catch(function (err) {
debugHelper.error(err);
});
Above is the code snippet that is in my program. And it does not send put request. While using postAsync, if will send post request successfully.
Your code seems fine to me.
Example
var request = require('request');
var Promise = require('bluebird');
Promise.promisifyAll(require("request"));
request.putAsync({
uri: 'https://httpbin.org/put',
headers: {
'content-type': 'application/json'
},
body: JSON.stringify({
name: 'anon'
})
}).spread(function (response, body) {
console.log(body);
}).catch(function (err) {
console.error(err);
});
OR you can just pass JSON body like this -
request.putAsync({
uri: 'https://httpbin.org/put',
json: { name: 'anon' }
})
....
Make sure the API end-point is taking PUT requests and the variables BU,formattedWorkStaions[BU] are properly defined. I guess formattedWorkStaions should be formattedWorkStations?