Problem with requesting XML in Node.js from specific website - node.js

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

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

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.

Calling https rest api using node js - request npm

We are trying to access a HTTPS REST API(GET) using nodeJS with request module, but it is throwing 401 - unauthorized.
We tried various options - provided like oauth_token OR user id & password, everything returns the same 401 status. I have given below the code snippet any suggestions would be helpful.
Note: We are running behind a proxy and we have set http proxy env variables, not sure if this will have anything to do with 401 (I don't think so but I am not 100 percent sure)
var request = require('request');
var options = {
uri: 'https://www.yammer.com/api/v1/messages.json',
auth: {
user: 'test',
pass: 'test-1',
sendImmediately: false
}
}
request(options, function (error, response, body) {
if (!error && response.statusCode == 200) {
console.log("Response is " + response);
} else {
console.log("Error is" + error);
console.log("Body is" + body);
console.log("Full response is" + JSON.stringify(response, null, 2));
}
});
Thanks.

GET request in Node.js always returns the same

I´m writing a Node.js app which should make an HTTP request using "request" module and save the response in Parse with some parameters and so. I use setInterval() for the loop.
Problem is I'm getting always the same response, like it´s cached or something. If I do a cURL form my local machine I see the actual data, however the loop in Node.js seems to get always the same response.
EDIT with code:
//Loop
setInterval(function(){
try {
foo.make_request();
}catch(e){
console.log(e);
}
}, 30 * 1000); //30 secs
and my make_request function:
function _make_request(){
//Configure the request
var options = {
url: 'http://player.rockfm.fm/rdsrock.php',
method: 'GET'
};
//Start the request
request(options, function (error, response, body) {
if (!error && response.statusCode == 200) {
// Print out the response body
var artist = body.substring(0, body.indexOf(':'));
var title = body.substring(body.indexOf(':')+3, body.indexOf('#')-1);
console.log(artist + " - " + title);
//upload to Parse etc etc
}
});
}
module.exports.make_request = _make_request;
Yeah! I left it working all the afternoon and it worked well:
request(options, function (error, response, body) {
response.on('data', function() {});
if (!error && response.statusCode == 200) {
// Print out the response body
var artist = body.substring(0, body.indexOf(':'));
var title = body.substring(body.indexOf(':')+3, body.indexOf('#')-1);
console.log(artist + " - " + title);
//upload to Parse etc etc
}
});
The solution was to actually consume the response with .on() method. As it turns out, you need to do it when throwing many request at the same time.

invalid oauth2 token request

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.

Resources