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";
Related
kind of a noob question, hopefully someone can help me with.
I am making an application which uses express.js to communicate with bitcoin-core by sending http reqests.
router.get("/getbalance", (req, res) => {
var dataString = `{"jsonrpc":"1.0","id":"curltext","method":"getbalance","params":[]}`;
var options = {
url: `http://${USER}:${PASS}#127.0.0.1:18443/`,
method: "POST",
headers: headers,
body: dataString
};
callback = (error, response, body) => {
if (!error && response.statusCode == 200) {
const data = JSON.parse(body);
res.send(data);
}
};
request(options, callback);
So far it is working quite well, and the problem arises when i try to pass more than one argument to the request, e.g., when i try to send bitcoin to an address in the console, the command is:
bitcoin-cli -regtest sendtoaddress [someaddress] [amount]
i tried to do it like this:
http://localhost:4444/api/sendtoaddress/bcrt1q3nczv7jr88rwvhsyv2rx49l3czfxurzfk240ue/10
but it just freezes.
This is what the "sendtoaddress" request looks like so far.
router.get("/sendtoaddress/:addr/:amount", (req, res) => {
var dataString = `{"jsonrpc": "1.0", "id": "curltext", "method": "sendtoaddress", "params": [${req.params.addr}, ${req.params.amount}]}`
console.log(req.params.addr)
var options = {
url: `http://${USER}:${PASS}#127.0.0.1:18443/`,
method: "POST",
headers: headers,
body: dataString
};
callback = (error, response, body) => {
if (!error && response.statusCode == 200) {
const data = JSON.parse(body);
res.send(data);
}
};
request(options, callback);
As I'm a new contributor so can't create a comment.
it would be better if u could use query method to get the data from the url.
A basic get url would look like this:
"https://xyc.com/vehicle?name="abc"&color="color"
query method is used to get the fields after the ?
We use req.params to get the :id from the url.
you can try changing your get url from this:
"/sendtoaddress/:addr/:amount"
to this:
"/sendtoaddress/:addr?amount="some_amount"
I hope this helps.
I have a problem with getting the xml from a get-request from this URL: https://feeds.meteoalarm.org/feeds/meteoalarm-legacy-atom-austria
In the browser, it all works fine, and also when I check the content on https://reqbin.com/, I get as a response a nice xml.
When I run my code, I just get a 404 status code back:
const request = require('request');
var urlAtom = 'https://feeds.meteoalarm.org/feeds/meteoalarm-legacy-atom-austria'
request.post({
url: urlAtom,
timeout: 8000
}, function(error, response, body){
if (error){
adapter.log.error(error)
)
}
if (response.statusCode == 200){
adapter.log.info('Status Code:' + response.statusCode)
}
else{
adapter.log.warn('Status Code:' + response.statusCode)
}
});
I tried it with another URL, there I get a 200 status code, so it doesn't seem connected to my device. I am not sure if this server requests any special parameters or so (I already tried playing around with useragend). I would be happy about any idea.
It's a GET request, So change request.post to request.get,
const request = require('request');
const urlAtom = 'https://feeds.meteoalarm.org/feeds/meteoalarm-legacy-atom-austria'
const adapter = { log: console }
request.get({
url: urlAtom,
timeout: 8000
}, function (error, response, body) {
if (error) {
adapter.log.error(error)
}
if (response.statusCode == 200) {
adapter.log.info('Status Code:' + response.statusCode)
}
else {
adapter.log.warn('Status Code:' + response.statusCode)
}
});
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.
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);
I'm developing a node application which needs to authenticate with google. When I request a token, https://accounts.google.com/o/oauth2/token responds with:
error: 400
{
"error" : "invalid_request"
}
I've tried making the same request in curl, and have received the same error, so I suspect there is something wrong with my request but I can't figure out what. I've pasted my code below:
var request = require('request');
var token_request='code='+req['query']['code']+
'&client_id={client id}'+
'&client_secret={client secret}'+
'&redirect_uri=http%3A%2F%2Fmassiveboom.com:3000'+
'&grant_type=authorization_code';
request(
{ method: 'POST',
uri:'https://accounts.google.com/o/oauth2/token',
body: token_request
},
function (error, response, body) {
if(response.statusCode == 201){
console.log('document fetched');
console.log(body);
} else {
console.log('error: '+ response.statusCode);
console.log(body);
}
});
I've triple checked to make sure all the data I'm submitting is correct and i'm still getting the same error. What can I do to debug this further?
It turns out that request.js (https://github.com/mikeal/request) doesn't automatically include the content-length to the headers. I added it manually and it worked on the first try. I've pasted the code below:
exports.get_token = function(req,success,fail){
var token;
var request = require('request');
var credentials = require('../config/credentials');
var google_credentials=credentials.fetch('google');
var token_request='code='+req['query']['code']+
'&client_id='+google_credentials['client_id']+
'&client_secret='+google_credentials['client_secret']+
'&redirect_uri=http%3A%2F%2Fmyurl.com:3000%2Fauth'+
'&grant_type=authorization_code';
var request_length = token_request.length;
console.log("requesting: "+token_request);
request(
{ method: 'POST',
headers: {'Content-length': request_length, 'Content-type':'application/x-www-form-urlencoded'},
uri:'https://accounts.google.com/o/oauth2/token',
body: token_request
},
function (error, response, body) {
if(response.statusCode == 200){
console.log('document fetched');
token=body['access_token'];
store_token(body);
if(success){
success(token);
}
}
else {
console.log('error: '+ response.statusCode);
console.log(body)
if(fail){
fail();
}
}
}
);
}
from here How to make an HTTP POST request in node.js? you could use querystring.stringify to escape query string of request parameters. Plus you'd better add 'Content-Type': 'application/x-www-form-urlencoded' for POST request.
post here the final string generated from token_request var.that may have something wrong. or may be authentication code is expired or not added correctly to the URL. Usually code has '/' in it that needs to escaped.