I am trying to list all jobs with a certain tag on angel.co using their API with the current call
https://api.angel.co/1/tags/10/startups
and then trying to parse it and show in the browser using restify
var tagUrl = "https://api.angel.co/1/tags/10/startups"
request({
url: tagUrl,
json: true
}, function (error, response, body) {
if (!error && response.statusCode === 200) {
console.log(body) // Print the json response
}
else console.log("error" + error)
})
I'm getting the console.log(body)-part to work, but when I am trying to send the response to the browser it doesn't show anything when I am trying to send it with
res.send('hello ' + req.params.name + body);
Should I parse or stringify it in some way ?
edit: This is the final code
function respond(req, res, next) {
var tag = req.params.tag;
var url = "http://api.angel.co/1/tags/"+tag+"/startups/?
request({
url: url,
json: true
}, function (error, response, body) {
if (!error && response.statusCode === 200) {
console.log(body) // Print the json response
res.send( req.params.name + JSON.stringify(body));
}
else console.log("error" + error)
})
Use stringify property of json,which converts the json format to string
JSON.stringify(body)
Related
I'm using the request package to send a post request to get an authToken, and then a get request to get an array of parcel objects so I can send it on to the rest of my app. When I log the body.parcels, it shows me several [Object, Object] items. I can't see the data inside of these objects, so I'm having trouble figuring out what the data is.
var options = {
uri: 'https://api.onetracker.app/auth/token',
method: 'POST',
json: {
email: this.config.username,
password: this.config.password,
},
};
request(options, function (error, response, body) {
if (!error && response.statusCode == 200) {
const authToken = body.session.token;
const options = {
uri: 'https://api.onetracker.app/parcels',
method: 'GET',
json: true,
headers: {
'x-api-token': authToken,
},
};
request(options, function (error, response, body) {
if (!error && response.statusCode == 200) {
let result = body.parcels;
console.log('Result: ' + result); // check
this.sendSocketNotification('ONETRACKER_RESULT', result);
}
});
}
You need to change your print line to:
console.log('Result: ', result); // check
See: How can I display a JavaScript object?
I am trying to use openweathermap.org weather api, I am getting the data but having trouble parsing it.
Here's my code:
request("https://api.openweathermap.org/data/2.5/weather?lat=30.4831&lon=76.595&appid=7beb6de85d3f3a28dabda1015684562f&units=metric&callback=test", function(error, response, body) {
if (!error && response.statusCode == 200) {
var parsedData = JSON.parse(body);
console.log(typeof body);
}
});
console.log(typeof body); returns string so I just can't figure out what 's the problem.
You are looking for this - the URL is JSONP so it expects a function called test and no need to parse
<script>
const test = data => console.log(data);
</script>
<script src="https://api.openweathermap.org/data/2.5/weather?lat=30.4831&lon=76.595&appid=7beb6de85d3f3a28dabda1015684562f&units=metric&callback=test"></script>
Alternatively remove the callback - here using fetch:
fetch("https://api.openweathermap.org/data/2.5/weather?lat=30.4831&lon=76.595&appid=7beb6de85d3f3a28dabda1015684562f&units=metric")
.then(response => response.json())
.then(data => console.log(data));
Or Axios
axios.get('https://api.openweathermap.org/data/2.5/weather?lat=30.4831&lon=76.595&appid=7beb6de85d3f3a28dabda1015684562f&units=metric')
.then(response => console.log(response.data));
<script src="https://unpkg.com/axios/dist/axios.min.js"></script>
This worked for me. Just set object for the first argument with url and json property. set json:true. Setting to true will handle the parsing for you :)
const request = require('request');
const url = "https://api.openweathermap.org/data/2.5/weather?lat=30.4831&lon=76.595&appid=7beb6de85d3f3a28dabda1015684562f&units=metric";
request({ url: url, json: true }, function (error, response, body) {
if (!error && response.statusCode == 200) {
console.log(body);
}
});
As #mplungjan pointed out. That's a JSONP url.
Remove &callback=test at the end of your URL then you don't have to deal with JSONP and you can work with it like normal.
Since you are using an API which has a callback function return as response. It is not JSON but JSONP (JSON with Padding).
Your code will work just fine if you remove the callback parameter from URL.
request("https://api.openweathermap.org/data/2.5/weather?lat=30.4831&lon=76.595&appid=7beb6de85d3f3a28dabda1015684562f&units=metric", function(error, response, body) {
if (!error && response.statusCode == 200) {
console.log(response);
}
});
For further reading about JSONP, You can refer this.
I'm trying to develop a currency converter using node.js. I'm using 'request' to make HTTP requests.
Currently in my code, the query strings (q=1, from=USD, to=LKR) are hard coded in the url.
I want to know how to pass those strings as arguments in order to make it dynamic and get as many currency formats as I want.
var request = require('request');
const options = {
url : "https://currency-exchange.p.rapidapi.com/exchange?q=1&from=USD&to=GBP",
headers: {
'x-rapidapi-host': 'currency-exchange.p.rapidapi.com',
'x-rapidapi-key': 'b13c4f3d67msh8143a7f1298de7bp1e8586jsn4453f885a4e7'
}
}
function callback(error, response, body) {
if (!error && response.statusCode == 200) {
var info = JSON.parse(body);
console.log(response.body);
}
}
request(options, callback);
You can use the qs parameter in the request library when performing a new request.
As specified here https://stackoverflow.com/a/16903926/7088387
You could use this:
const params = {
q: 1,
from: 'USD',
to: 'GBP'
};
const options = {
url : "https://currency-exchange.p.rapidapi.com/exchange",
headers: {
'x-rapidapi-host': 'currency-exchange.p.rapidapi.com',
'x-rapidapi-key': 'b13c4f3d67msh8143a7f1298de7bp1e8586jsn4453f885a4e7'
},
qs: params
}
function callback(error, response, body) {
if (!error && response.statusCode == 200) {
var info = JSON.parse(body);
console.log(response.body);
}
}
request(options, callback);
You could have a variable that stores those:
var currencies = ['USD', 'GBP'];
And then just put those variable values into your request string:
url : "https://currency-exchange.p.rapidapi.com/exchange?q=1&from=" + currencies[0] + "&to=" + currencies[1]
You could also use template literals using backticks like so:
url : `https://currency-exchange.p.rapidapi.com/exchange?q=1&from=${currencies[0]}&to=${currencies[1]}`
I am tryng to send an array as a param so I have the following code
var myJsonString = JSON.stringify(url);
request.get('http://localhost:8090/saveURL/'+myJsonString, function(req, res,body) {
console.log(body);
})
How I can send params with node ?
EDIT
I changue my request petition , now I am using this.
request.get({
url: 'http://localhost:8090/saveURL/',
qs: { param1: JSON.stringify(url)}
},
function(req, res,body) {
console.log(req);
console.log(body);
}
)
This is correct ?
According the the request docs, you can use qs to pass an object of query params:
request.get({
url: 'http://localhost:8090/saveURL/',
qs: { param1; 'a', param2: 'b'}
},
function(req, res,body) {
console.log(body);
}
)
I'd recommend performing this with POST method and not GET, which is more appropriate for altering data on the server.
Also, the URL length is limited and if your data is very large, it won't work.
For instance:
request.post(
'http://localhost:8090/saveURL/',
{ json: { key: 'value' } },
function (error, response, body) {
if (!error && response.statusCode == 200) {
console.log(body)
}
}
);
Replace { key: 'value' } with a POJO (a normal JS object).
Using the request node library to make GET requests and and wondering how to grab the URL from the response returned.
Something like
var request = require('request');
request('http://www.somewebsite.com', function (error, response, body) {
if (!error && response.statusCode == 200) {
console.log(body) // grab the url here
}
})
assuming the response returned is not www.somewebsite.com but a redirect.
Start at the documentation. Search it for redirect and you'll find:
followRedirect - follow HTTP 3xx responses as redirects (default: true). This property can also be implemented as function which gets response object as a single argument and should return true if redirects should continue or false otherwise.
Note the status code. You only get the 200 after the redirect has been followed.
Therefore:
var request = require('request');
request({
uri: 'http://google.com',
followRedirect: function(response) {
console.log("Redirecting to " + response.headers.location);
return true;
}
},
function(error, response, body) {
if (!error && response.statusCode == 200) {
console.log(body)
}
}
);