I am building an app using node.js and am using the request
package to make requests. I am trying to pass data to my server API to return all orders - my question is how can I send parameters as part of my request? My code so far:
function callback(error, response, body) {
console.log(body); //returns empty; should return array of orders
res.render('../views/orders');
}
var options = {
url: 'http://localhost:3000/api/orders',
headers: {
host: 'localhost:3000',
connection: 'close',
cookie: cookieText
},
data: { //data and company_id are parameters I want to pass
email: 'abc123@gmail.com',
company_id: 'secretcompanyid'
}
};
request(options, callback);
In my code, email
and company_id
are the parameters my server side API needs. However, the current request is not returning any data. When I console.log the parameters on the server side, it returns empty therefore I think the issue is how I am passing parameters to the server. Can someone help?
Thanks in advance!
Do it like this since its a GET request:
function callback(error, response, body) {
console.log(body); //returns empty; should return array of orders
res.render('../views/orders');
}
var options = {
url: 'http://localhost:3000/api/orders',
headers: {
host: 'localhost:3000',
connection: 'close',
cookie: cookieText
},
qs: { //data and company_id are parameters I want to pass
email: 'abc123@gmail.com',
company_id: 'secretcompanyid'
}
};
request(options, callback);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.