Node.js http Request timeout error - node.js

I'm using npm request module to call an http api. It's working for 1 url but not working for same domain other url, everytime I'm getting timeout. same url is working in postman. What could be the reason ?
var request = require("request");
const url1 = 'http://example.com/xyz';
const url2 = 'http://example.com/jkl';
var options = {
method: 'GET',
url: url,
headers: {
Host: 'example.com',
'User-Agent': 'request'
}
};
request(options, function(error, response, body) {
if (error) throw new Error(error);
console.log(body);
});

To handle timeout issues, you can choose the following way:
var options = {
url: 'http://your_url',
timeout: 120000 // you can set this value as per your needs
}
request(options, function(err, resp, body) {});

Related

How to make json-rpc request with Adonis

Am using Adonis to build a Bitcoin RPC system, so am making request with request.js Lib, so but the issue is with the callback when I make the request it works but I can't see send the response to the web endpoint, when I console the response from the RPC server it works fine but on postman it is blank.
getBlockCount({ response}){
const dataString = `{"jsonrpc":"1.0","id":"curltext","method":"getblockcount","params":[]}`;
const options = {
url: `http://${USER}:${PASS}#${HOST}:${PORT}/`,
method: "POST",
headers: headers,
body: dataString
};
const returnData;
const callback = (error, nextRes, body) => {
if (!error && nextRes.statusCode == 200) {
const data = JSON.parse(body);
console.log(data)
returnData = data;
response.status(200).send(returnData)
}
return response.send('data');
};
return request(options, callback);
// const options = requestOption(dataString);
// console.log(rpcRequest(options, callBack(response)));
}
I ended up using request-promise
And this is what it look like
async getBlockCount({req, response}){
return await rp(requestOption(`{"jsonrpc":"1.0","id":"curltext","method":"getblockcount","params":[]}`))
}
function requestOption(dataString) {
return {
url: `http://${USER}:${PASS}#${HOST}:${PORT}/`,
method: "POST",
headers: headers,
body: dataString
};
}

HTTP POST with node.js and request library isn't outputting anything

I'm just testing to see if my POST request to a website works, but it isn't outputting anything. When I use RunKit, it shows an output, but not in my powershell. Am I doing something wrong or is there no output? How can I make it show the output? Here is my code:
var request = require('request');
request.post(
'My_API_URL',
{ json: { "text":"this is my text" } },
function (error, response, body) {
console.log(body);
}
);
What i suggest you, is to update your code like this and try again:
var request = require('request');
var options = {
uri: 'My_API_URL',
method: 'POST',
json: {
"text":"this is my text"
}
};
request(options, function (error, response, body) {
if (!error && response.statusCode == 200) {
console.log(body);
}
});
Let me know if the result is the same.
Check this link. You should do post requests like this:
var request = require('request');
var body = JSON.stringify({
client_id: '0123456789abcdef',
client_secret: 'secret',
code: 'abcdef'
});
request.post({
url: 'https://postman-echo.com/post',
body: body,
headers: {
'Content-Type': 'application/json'
}},
function (error, response, body) {
console.log(body);
}
);

How to download GitHub release asset from private repo using node

I would like to use node.js request module to download a release asset from a private repo. It works fine with the following cURL command:
curl -O -J -L \
-H "Accept: application/octet-stream" \
https://__TOKEN__:#api.github.com/repos/__USER__/__REPO__/releases/assets/__ASSET_ID__
but it fails when I try using the request module:
var request = require('request');
var headers = {
'Accept': 'application/octet-stream'
};
var API_URL = "https://__TOKEN__:#api.github.com/repos/__USER__/__REPO__"
var ASSET_ID = __ASSET_ID__
var options = {
url: `${API_URL}/releases/assets/${ASSET_ID}`,
headers: headers,
};
function callback(error, response, body) {
if (error) {
console.error(error);
}
console.log(response.statusCode)
if (!error && response.statusCode == 200) {
console.log(body);
}
console.log(options.url);
}
var req = request(options, callback);
console.log(req.headers)
I have double checked that the resulting URL when using node is the same as the one I use with cURL.
I receive a 403 statusCode in the response. I do not understand why.
UPDATE:
While looking at the headers that are actually sent, I have found that the it uses
{ Accept: 'application/octet-stream',
host: 'api.github.com',
authorization: 'Basic __DIFFERENT_TOKEN__' }
I do not understand why the token is changed.
some references: https://gist.github.com/maxim/6e15aa45ba010ab030c4
GitHub API requires a user agent (https://github.com/request/request#custom-http-headers)
It is also important to set the encoding to null in order to have a buffer and not a string in the body (Getting binary content in Node.js using request)
The working version of the code is thus:
var request = require('request');
var headers = {
'Accept': 'application/octet-stream',
'User-Agent': 'request module',
};
var API_URL = "https://__TOKEN__:#api.github.com/repos/__USER__/__REPO__"
var ASSET_ID = __ASSET_ID__
var options = {
url: `${API_URL}/releases/assets/${ASSET_ID}`,
headers: headers,
encoding: null // we want a buffer and not a string
};
function callback(error, response, body) {
if (error) {
console.error(error);
}
console.log(response.statusCode)
if (!error && response.statusCode == 200) {
console.log(body);
}
console.log(options.url);
}
var req = request(options, callback);
console.log(req.headers)
Thanks to Marko Grešak.

Node js Request - Empty body in response

I am using node js request to retrieve the HTML from the following URL but the body is returning empty.
var request = require("request");
var url = 'http://www.topshop.com/en/tsuk/product/bags-accessories-1702216/scarves-465/feather-wings-5884878?bi=0&ps=20';
request({
uri: url
}, function (error, response, body) {
console.log(body);
if (response.statusCode != '200') {
console.log('fail');
console.log(response.statusCode + ' # ' + error);
} else {
console.log(response.statusCode);
console.log('############');
console.log(response);
}
});
On closer inspection I can see this in the response:
_header: 'GET /webapp/wcs/stores/servlet/CatalogNavigationSearchResultCmd?langId=-1&storeId=12556&catalogId=33057&beginIndex=1&viewAllFlag=false&pageSize=20&searchTermScope=3&searchTermOperator=LIKE&searchType=ALL&sort_field=Relevance&searchTerm=TS19M11KRED&x=25&y=11&geoip=search HTTP/1.1\r\nreferer: http://www.topshop.com/en/tsuk/product/bags-accessories-1702216/scarves-465/feather-wings-5884878?bi=0&ps=20&geoip=prod\r\nhost: www.topshop.com\r\nConnection: close\r\n\r\n',
_headers:
{ referer: 'http://www.topshop.com/en/tsuk/product/bags-accessories-1702216/scarves-465/feather-wings-5884878?bi=0&ps=20&geoip=prod',
host: 'www.topshop.com' },
Which I assume means that there has been a redirect? Even though its returned a 200 OK instead of a 302 redirect.
I'm not sure of the best way to retrieve the body from the redirect? Do I need to make another request to the URL in the header? But shouldn't the response code be a 302 in this case instead of a 200?
Any help appreciated.
What you show seem like something that happened after a redirect - see that the referer is set to your original URL.
Maybe you should set more headers, like User-Agent because some servers don't respond without it.
For example, see the code that I wrote for this answer:
'use strict';
var request = require('request');
var url = 'https://api.github.com/users/rsp';
request.get({
url: url,
json: true,
headers: {'User-Agent': 'request'}
}, (err, res, data) => {
if (err) {
console.log('Error:', err);
} else if (res.statusCode !== 200) {
console.log('Status:', res.statusCode);
} else {
// data is already parsed as JSON:
console.log(data.html_url);
}
});
It returns:
https://github.com/rsp
Note that it doesn't work without the User-Agent header:
'use strict';
var request = require('request');
var url = 'https://api.github.com/users/rsp';
request.get({
url: url,
json: true,
}, (err, res, data) => {
if (err) {
console.log('Error:', err);
} else if (res.statusCode !== 200) {
console.log('Status:', res.statusCode);
} else {
// data is already parsed as JSON:
console.log(data.html_url);
}
});
It returns:
Status: 403
The same URL, the same code - the only difference is the User-Agent header.

POST data with request module on Node.JS

This module is 'request https://github.com/mikeal/request
I think i'm following every step but i'm missing an argument..
var request = require('request');
request.post({
url: 'http://localhost/test2.php',
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...
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: 'http://localhost/test2.php',
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: 'http://localhost/test2.php',
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: 'http://localhost/test2.php',
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: 'https://api.site.com',
body: jsonDataObj,
json: true
}, function(error, response, body){
console.log(body);
});
var request = require('request');
request.post('http://localhost/test2.php',
{form:{ mes: "heydude" }},
function(error, response, body){
console.log(body);
});
Install request module, using npm install request
In 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: 'http://localhost/PhpPage.php',
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 = 'http://your-website-link.com/sample.php';
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 https://www.npmjs.com/package/axios install it with npm or yarn
const axios = require('axios');
axios.get('http://your_server/your_script.php')
.then( response => {
console.log('Respuesta', response.data);
})
.catch( response => {
console.log('Error', response);
})
.finally( () => {
console.log('Finalmente...');
});

Resources