Regarding to this question:
Transfer / pass cookies from one request to another in nodejs/protractor
I got another one. How could I view complete request (headers + body) which I am performing via nodejs?
Yes, you can ... You can access the complete request from the full response body - response.request
I have a generic full response structure illustrated below
IncomingMessage
ReadableState
headers(ResponseHeaders)
rawHeaders
request - //This is what you need
headers
body
body(Response Body)
You can access through code as shown below
var request = require("request");
var options = { method: 'POST',
url: 'http://1.1.1.1/login',
headers:
{ 'cache-control': 'no-cache',
'content-type': 'application/json' },
body: { email: 'junk#junk.com', password: 'junk#123' },
json: true };
request(options, function (error, response, body) {
if (error) throw new Error(error);
// This will give you complete request
console.log(response.request);
//This gives you the headers from Request
console.log(response.request.headers);
});
Related
I'm trying to use the request module to send post request to another service but the callback never fires. Here is what I'm doing right now:
request.post(
`http://localhost:3002/users/login`,
{
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({userDetails})
},
function (error, response, body) {
if (!error && response.statusCode == 200) {
const data = JSON.parse(body);
console.log(data);
} else {
console.log('Request has failed. Please make sure you are logged in');
res.status(401).send(body);
}
}
);
The callback function never fires. On the other hand, if I try to send this exact request with Postman, the server gets the request. What am I doing wrong?
Syntax error maybe? Try with:
request({
method: 'POST',
url: 'http://localhost:3002/users/login',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({userDetails})
},
function (error, response, body) {
// Now it should fire the callback.
console.log('okay');
});
Works like a charm on my side.
Edit: If it still doesn't work, my bet is that the cross-origin resource sharing is not enabled for the localhost:3002 server you're trying to access. Here's the easiest way to enable it.
I am sending s POST request to a service and it is supposed to return a JSON in response. I see the POST is successful but I dont get anything in response back. What am I missing? Below code
var headers = {
"Accept":"application/json",
"Content-Type":"application/json",
"Authorization": ("Basic " + new Buffer("admin:password").toString('base64'))
}
// Configure the request
var options = {
url: 'myservice-url',
method : 'POST',
headers : headers
}
// Start the request
request(options, function (error, response, body) {
if (!error && response.statusCode == 200) {
// Print out the response body
var str = JSON.stringify(body, null, 2);
console.log(str)
}
})
First of all, need to confirm if you have properly installed the following:
https://github.com/request/request
Then require it in your file like this:
var request = require('request');
request.post({
url: "",//your url
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
},
rejectUnauthorized: false,//add when working with https sites
requestCert: false,//add when working with https sites
agent: false,//add when working with https sites
form: {
whateverfieldnameyouwant: "whatevervalueyouwant"
}
},function (response, err, body){
console.log('Body:',JSON.parse(body));
}.bind(this));
When using request, how do you set the request body? The form parameter creates a body of key value pairs: foo=bar&baz=qux, but how do you set the body to an arbitrary string?
You can set request body in the options params.
const options = {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({test: 1});
};
request(options, function (err, res, body) {
// handle err or use response and response boy
});
Depending upon the content you want to send, you can provide the string needed.
How to integrate api with nodejs?
Using api,i want to get all the values in nodejs
I want a detail coding for this.
Thanks in advance
shobana you can get results from any APIs using a curl or http request. Following is a code snippet you can give a try.
var request = require('request');
// Set the headers
var headers = {
'User-Agent': 'Super Agent/0.0.1',
'Content-Type': 'application/x-www-form-urlencoded'
}
// Configure the request
var options = {
url: 'url_link',
method: 'POST',
headers: headers,
form: {"key1":value1,"key2":value2,"key3":value3}
}
// Start the request
request(options, function (error, response, body) {
if (!error && response.statusCode == 200)
{
// Print out the response body
console.log(body)
}
})
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.
Can anyone help explain why?
I already found the part where is wrong in the putAsync code snippet. I should use json not body as the key for the payload. And the payload does not need to be stringified.
Here is the new code snippet proven to work.
var request = require('request');
var Promise = require('bluebird');
Promise.promisifyAll(require("request"));
request.putAsync({
uri: buApiUrl,
headers: {
'content-type': 'application/json'
},
json: {
name: BU,
workstations: formattedWorkStaions[BU]
}
}).spread(function (response, body) {
debugHelper.log(body);
}).catch(function (err) {
debugHelper.error(err);
});
This is quite tricky and result in my second question.
Why there is such a difference between post and put other than their method type?