I have implemented a GET request via request module in Nodejs in an application. I need to display the response over html page.
However, the response returned is undefined. Also, error and body message is also undefined.
I tried running it seperately with "node file.js", it works fine. Do I have to add anything else while using it in application.
I also tried htttp, curl-request and request-promise modules, all of them return undefined.
var request = require(['request']);
var headers = {
'Accept': 'text/json',
'X-Auth-Token': tk,
'User-Agent': 'request'
};
var options = {
url: 'https://api.github.com/repos/request/request',
headers: headers,
method: 'GET',
json: true
};
function callback(error, response, body){
console.log(response);
console.log(error);
if (!error && response.statusCode == 200) {
console.log(body);
return body;
}
}
request(options, callback);
I just had the same issue and found out that the server certificate was not fully trusted. As a TEMPORARY solution you could try to add strictSSL: false to your options object.
If it works like this (and assuming that you did not really send the request to "https://api.github.com/repos/request/request") contact the service provider and tell them about the certificate issues.
Related
I have the following code an i try to connect to a server and get a json response from the server but somthing is wrong becouse i get this error: error null 401. Can you please help me to identify the error on this code or to find what is missing. Thank you!
var request = require('request');
var apiurl = "externalserverurl:6080";
var options = {
"regid" : "username",
"password" : "password",
"uri" : apiurl,
"registrar_domain" : "mydomain.com",
"lang" : "en",
json: true,
method: 'POST',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json',
'User-Agent': 'request'
}
};
function callback(error, response, body) {
if (!error && response.statusCode == 200) {
console.log('body:', body);
} else {
console.log('error', error, response && response.statusCode);
}
}
request(options, callback);
As #kay already mentioned, errorcode 401 means that your request is unauthorized. Given that there are a lot of different possible authentication methods (basic auth, digest auth, OAuth, you name it), you should refer to your target server and clarify which auth method you should use before trying to actually modify your code.
You could also check the response body, it could contain some hints about what exactly the server expects but doesn't receive.
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.
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)
}
})
currently my code looks like this. The problem is, it is serving me HTML instead of JSON. Am I querying the wrong location? Should I be using a package more sensible for handling this type of data? I just don't understand. Could anyone point me in the right direction? Thanks
var request = require('request');
var options = {
url: 'https://api.psychonautwiki.org/?query={ substances { name effects { name } }}',
headers: {
'Content-Type': 'application/json'
}
};
function callback(error, response, body) {
if (!error && response.statusCode == 200) {
console.log(body);
}
}
request(options,callback);
}
GraphQL apis generally get query and the rest of your options in as the request body, and mostly use the POST http method. It seems like this api reacts to your GET request with a GraphiQL user interface, that's why you are getting html as response.
Try adding an Accept header to tell the server what you're expecting back.
'Accept': 'application/json'
I've been trying to hit an API and get some data back from it (It's a free API not my own). So I've got my API token and I've had a look around and found that npm package request seems to be the best.
Within one of my routes I have,
request({
uri: "https://app.url-to-api:443/api/list-of-data",
method: "GET",
api_token: "my-api-token",
timeout: 10000,
followRedirect: true,
maxRedirects: 10
}, function(error, response, body) {
console.log(body);
});
So I'm getting "message":"Authorization has been denied for this request." back which is obviously because my API Token isn't getting passed through.
This might be a stupid question, but where do I actually put the API token to validate my request?
Thanks!
In request it would be something like this:
request.get('http://some.server.com/', {
'auth': {
'bearer': 'bearerToken'
}
});
More details on what you can do with request are in the docs.
You have to pass api tokens in request headers please see the documentation for request
var request = require('request');
var options = {
url: 'https://api.github.com/repos/request/request',
headers: {
'Access-Token': 'request'
}
};
function callback(error, response, body) {
if (!error && response.statusCode == 200) {
var info = JSON.parse(body);
console.log(info.stargazers_count + " Stars");
console.log(info.forks_count + " Forks");
}
}
request(options, callback);