nodejs use REQUEST.get, callback never called - node.js

I am using request module:
request.get({
url: 'https://www.google.com'
}, function (err, response, body) {
console.log('callback called!' );
I opened the request debug mode:
require('request').debug = true;
so i saw the debug output:
REQUEST { method: 'GET',
callback: [Function],
url: 'https://www.google.com' }
REQUEST make request https://www.google.com/
but the callback was never called
so, what's the problem? Is it relate to https?

Related

Node JS request get original url

I'm sending GET requests like this in Node JS in a loop
request({
url : 'https://somelink.com',
method: 'GET'
},
function (error, response, body) {
console.log(response);
});
Since the response is async, is it possible to get the original request URL in the response?
Thanks!
You can get the original request href in the response.request object, like so:
const request = require("request");
request({
url : 'https://google.com',
method: 'GET'
},
function (error, response, body) {
if (!error) {
console.log("Original url:", response.request.uri.href);
console.log("Original uri object:", response.request.uri);
}
});
You can access more information in the request.uri object, for example:
console.log("Original uri:", response.request.uri);
This will give you some more useful information like port, path, host etc.

Node.js request module

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.

Send a File From Api Server to NodeJs to Browser

I have an API Server and NodeJs Server and when a file is requested NodeJs redirected the request to API Server
API Server Send the File as raw data to NodeJs
and Nodejs redirects the file to the browser
But when I checked the network data using wire shark the packet received at browser is not original as that from API Server (work in case of text files, but not in image, video, pdf, doc etc)
router.get('/GetCaseSupportDocument', function (req, res) {
var MyJsonData = {
DocId:parseInt(req.query.DocId) || 0
};
request({
url: 'http://somedomain/someurl', //URL to hit
method: 'POST',
json: MyJsonData
}, function (error, response, body) {
if (error) {
res.status(200).send('Failed');
} else {
res.status(200).send(body);
}
})
});
Can anyone tell why it changes between NodeJs to Browser?
Is there any better solution for this type of transmission?
Updated After finding solution . This works
router.get('/GetCaseSupportDocument', function (req, res) {
var MyJsonData = {
DocId:parseInt(req.query.DocId) || 0
};
request({
url: Url.CaseService + 'GetCaseSupportDocument', //URL to hit
method: 'POST',
json: MyJsonData
}).pipe(res);
})
There is a simple proxy using streams that you can try:
router.get('/GetCaseSupportDocument', function (req, res) {
var MyJsonData = {
DocId: parseInt(req.query.DocId) || 0
};
// updated the response
request({
url: 'http://somedomain/someurl', //URL to hit
method: 'POST',
json: MyJsonData
}).pipe(res);
});
More details with proxy-ing you can find on the request documentation https://github.com/request/request

How to view request sent from node.js to server?

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);
});

302 Redirect prevents callback in Request

I've noticed some websites don't close 302 redirects which causes the callback not to fire.
Anyone know how to remedy this?
var request = require('request');
request({url:'http://craigslist.org' }, function (error, response, body)
{
console.log("Callback Never Runs. ");
});
Sorry, upon further investigation this suggestion also fails. But when trying multiple times with your code that will work occasionally too. The best I've come up with is using a timeout to retry. timeout fires the complete event where you can check if the request really finished and if not retry.
var request = require('request');
function doRequest(location) {
request({
timeout: 5000, // five seconds
url:location
}, function (error, response, body) {
console.log("Callback Never Runs. ");
}).on("complete", function(response) {
if(!response.complete) setImmediate(function() {
doRequest(location);
});
});
}
doRequest('http://craigslist.org');
You can expand on this further to only retry X number of times before throwing an error.
----original----
This could be a bug in request as in this case craigslist.org sends all 302 errors. But if you add followAllRedirects: true to you options it works.
var request = require('request');
request({
followAllRedirects: true,
url:'http://craigslist.org'
}, function (error, response, body) {
console.log("Callback Never Runs. ");
});
It should work if you include a User-Agent (such as your browser's):
request({
url: 'http://craigslist.com',
headers: {
'User-Agent': 'Mozilla/5.0 (...) ...'
}
}, function (err, res, body) {
// ...
});
Without that, Craigslist seems to leave the connection open, so it doesn't 'end' or 'complete' and the callback isn't called.

Resources