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'
Related
I have the following API response named "body" and trying to log a specific field/submap, for example the "response" map. I don't really know the correct syntax in NodeJS or JavaScript.
{"get":"fixtures","parameters":{"league":"135","season":"2022","status":"NS","timezone":"Europe\/rome"},"errors":[],"results":40,"paging":{"current":1,"total":1},"response":[{"fixture":{"id":881790,"referee":"M. Piccinini","timezone":"Europe\/rome","date":"2022-08-20T18:30:00+02:00","timestamp":1661013000,"periods":{"first":null,"second":null},"venue":{"id":943,"name":"Stadio Olimpico Grande Torino","city":"Torino"},"status":{"long":"Not Started","short":"NS","elapsed":null}},"league":{"id":135,"name":"Serie A","country":"Italy","logo":"https:\/\/media.api-sports.io\/football\/leagues\/135.png","flag":"https:\/\/media.api-sports.io\/flags\/it.svg","season":2022,"round":"Regular Season - 2"},"teams":{"home":{"id":503,"name":"Torino","logo":"https:\/\/media.api-sports.io\/football\/teams\/503.png","winner":null},"away":{"id":487,"name":"Lazio","logo":"https:\/\/media.api-sports.io\/football\/teams\/487.png","winner":null}},"goals":{"home":null,"away":null},"score":{"halftime":{"home":null,"away":null},"fulltime":{"home":null,"away":null},"extratime":{"home":null,"away":null},"penalty":{"home":null,"away":null}}},]}
The code is below:
var request = require("request");
var options = {
method: 'GET',
url: 'https://v3.football.api-sports.io/fixtures?league=135&season=2022&status=NS&timezone=Europe/rome',
qs: {},
headers: {
'x-rapidapi-host': 'v3.football.api-sports.io',
'x-rapidapi-key': 'xxxxxxxxxxxxxxxxx'
}
};
request(options, function (error, response, body) {
if (error) throw new Error(error);
console.log(body);
});
I tried console.log(body.response); without success. I also tried body["response"] ending with "undefined" result.
It seems body was a String result so I could select the field with JSON syntax. I solved with console.log(JSON.parse(body).response[0]);
Two approaches:
Approach 1: Set the Content-Type as application/json in the server.
Approach 2: Convert the body from string to JSON object by JSON.parse
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.
Hello guys i want to use node.js to retrieve the user authenticated from an api URL.
I retrieved the codeToken(jsonwebtoken) and put it in the header of my request in postman it works.
But when i go to the code i can't find a way to use node.js+express request, to retrieve the json response of the user that's logged in using all that.
i can't put the url of the API in the sake of the client.
here is my code guys but it's not the right one please advise
var request = require('request');
var options = {
url: '**********',//for the sake of client hidden
headers: {
tokenCode: '15288648455b20a04d5463e'
}
};
function callback(error, response, body) {
if (!error && response.statusCode == 200) {
var info = JSON.parse(body);
console.log(info);
}else
//console.log(response );
console.log(response.statusCode);
}
request(options, callback);
Headers in my POSTMAN (only tokenCode)
i am trying to make a GET request to an external api from node js and express js.
I am using the request module to make the GET request-
in the options of the request -
const options = {
url: 'https://externalP_api_url/api/1/balance',
method: 'GET',
headers: {
'Content-Type': 'application/json'
}
};
I need to set the -u parameter as based on the example in the docs of the api. they used curl to make the request
curl -u keyid:keysecret https://externalP_api_url/api/1/balance
The problem is i cant set the -u keyid:keysecret in the options of my request i have tried putting it like a query string and in the header but i get a 401 and accoring to the api's docs that means the keyid:keysecret parameter is missing.
I have tried the curl and it works so i knw its not from the api's end and its not cause i am not sending from an https doamain because i would have a gotten a 403 response.
Take a look at https://curl.trillworks.com/#node
It typically does a pretty good job of pointing you in the right direction when converting curl to node.js. Based on your curl command, it is suggesting the following code:
var request = require('request');
var options = {
url: 'https://externalP_api_url/api/1/balance',
auth: {
'user': 'keyid',
'pass': 'keysecret'
}
};
function callback(error, response, body) {
if (!error && response.statusCode == 200) {
console.log(body);
}
}
request(options, callback);
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);