Consuming a service API using nodejs + jsonwebtoken(jwt) - node.js

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)

Related

How to use query paramaters for spotify api using node.js?

var queryparam = "track:godsplan%20artist:drake&type=track&market=US&limit=10";
app.get('/get_track', function(req,res){
var options = {
url:"https://api.spotify.com/v1/search?"+queryparam,
headers: { 'Authorization': 'Bearer ' + access_token },
json: true
}
request.get(options, function(error, response, body) {
if(!error && response.statusCode === 200) {
console.log(body);
}
else{
console.log(error);
console.log(response.statusCode);
}
res.redirect('/#');
});
});
here is what I have and i am trying to get god's plan by drake to appear on my console when I run the server on local host and my access_token works but whenever i run the "/get_track" i get a bad request error, does anyone know why?
Your missing q=
var queryparam = "q=track:godsplan%20artist:drake&type=track&market=US&limit=10";
https://developer.spotify.com/documentation/web-api/reference/search/search/#request-parameters
what is that module "request" ?
(http, request-promise, request)
although I suppose that your request is simply not correctly formed
you need to try:
var queryparam = "q=track:godsplan%20artist:drake&type=track&market=US&limit=10";

From Node.js how to call REST API that uses ASP.NET web API windows authentication?

I am using request npm package to call api that uses windows authentication.
I make call to GET method as below. But I get 401 UNAUTHORIZED as response code.
var request = require('request');
var options = {
url: 'http://url',
method : 'GET',
json : true,
headers: {
'Authorization': 'NTLM MpJfHMQjKT4V8txCgAQAAAAAAAAAAAAAAAAAAAAAAAJAFIASABUAFQAUAAvAGgAcQBtAGUAcgBjAGgAcwB2AGMAMAAxAHQAMQBiAC4AZABzAHQAZQBzAHQALgBkAHIAdQBnAHMAdABvAHIAZQAuAGMAbwBtAAAAAAAAAAAAAAAAAEZDtgUbISIZWdfhvrcI7aujEgQQAQAAAJTRhB/tA+DyAAAAAA=='
}
};
function callback(error, response, body) {
console.log(response.statusCode);
if (!error && response.statusCode == 200) {
var info = JSON.parse(body);
}
}
request(options, callback);
Please help me, how to call API that uses ASP.NET web API windows authentication using node.js ?
Thanks in advance!

Insert data through SPARQL over HTTP POST request from node js

I am using 'request' module in my node app to POST data in ontology model which resides in a fuseki server. I am using the following code:
var request = require('request');
var querystring = require('querystring');
var myquery = querystring.stringify({update: "PREFIX test:<http://www.semanticweb.org/muhammad/ontologies/2017/2/untitled-ontology-14#> INSERT { ?KPIs test:hasValue 2009} WHERE { ?KPIs test:hasValue ?Newvalue}"});
request.post('http://localhost:3030/DS-1/sparql?'+myquery, function (error, response, body) {
if (!error && response.statusCode == 200) {
// Show the HTML for the Google homepage.
console.log('successful update');
console.log(body);
} else {
console.log(response.statusCode);
console.warn(error);
}
});
PS: When I use POSTMAN to send the Post request to insert data it works fine but from my node app, it doesn't. it shows error 'bad request 400'.
P.S: GET methods work fine from both POSTMAN and node app.
Problem Solved:
I was making mistake in the format of post request. The corrected format is given below.
var request = require('request');
var querystring = require('querystring');
var myquery2 = querystring.stringify({update: "PREFIX test:<http://www.semanticweb.org/muhammad/ontologies/2017/2/untitled-ontology-14#> INSERT { ?KPI_Variables test:hasValue_ROB1 2000} WHERE { ?KPI_Variables test:hasValue_ROB1 ?Newvalue FILTER(?KPI_Variables= test:Actual_Production_Time)}"});
request.post({headers: {'content-type' : 'application/x-www-form-urlencoded'},url:'http://localhost:3030/DS-1/?'+myquery2 }, function (error, response, body) {
if (!error && response.statusCode == 200) {
// Show the HTML for the Google homepage.
console.log('successful update');
console.log(body);
}
else
{
console.log(response.statusCode)
console.warn(error);
}
});
I was missing the 'headers' and 'url' elements in my request.post.
/DS-1/sparql is the query service.
INSERT is an update operation.
Try /DS-1/update
It is better to POST the update in the body of the request with a Content-type. ?update= may not work.

Sending an API request with Nodejs

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

Trigger a test http request

For unit testing purposes I want to trigger a fake http request in expressjs to my routes to test how each respond. Is there an easy way to do this?
Trigger a real request. it will not use any bandwidth since its on the same machine
var options = {
hostname: 'example.com',
path: '/test'
};
http.request(options).end();
Request.js is the most common way to send requests with node.
var request = require('request');
request('http://www.google.com', function (error, response, body) {
if (!error && response.statusCode == 200) {
console.log(body) // Print the google web page.
}
})

Resources