Passing query parameters to GET request (with request npm module) - javascript

I have this function below that is supposed to fetch data from a Twitch API Endpoint,
But I need to pass a query parameter with a key to_id and a value, and I don't know how to pass query parameters using this npm module,
I think I have to do this in the gameOptions object below,
(Here's a link of the doc: https://www.npmjs.com/package/request#requestoptions-callback)
function gameRequest(accessToken) {
setTimeout(() => {
const gameOptions = {
url: "https://api.twitch.tv/helix/users/follows",
method: "GET",
headers: {
"Client-ID": MY_CLIENT_ID,
Authorization: "Bearer " + accessToken,
},
};
if (!accessToken) {
console.log("No Token");
} else {
console.log(gameOptions);
const gameRequest = request.get(gameOptions, (err, res, body) => {
if (err) {
return console.log(err);
}
console.log(JSON.parse(body));
});
}
}, 2000);
}

You can user qs in gameOptions :
const gameOptions = {
url: "https://api.twitch.tv/helix/users/follows",
method: "GET",
qs : {
"to_id" : valueToPass
},
headers: {
"Client-ID": MY_CLIENT_ID,
Authorization: "Bearer " + accessToken,
},
};

Related

Access the response of Node.JS Post Request

I'm trying the wrap my head around the Client Credentials Flow of Spotify API and in their documentation they have this way to get the Access Token:
var client_id = 'CLIENT_ID';
var client_secret = 'CLIENT_SECRET';
const authOptions = {
url: "https://accounts.spotify.com/api/token",
headers: {
"Authorization":
"Basic " +
new Buffer.from(clientID + ":" + clientSecret).toString("base64"),
"Content-Type": "application/x-www-form-urlencoded",
},
form: {
grant_type: "client_credentials",
},
json: true,
};
request.post(authOptions, function(error, response, body) {
if (!error && response.statusCode === 200) {
var token = body.access_token;
}
});
Now I'm trying to get that token and export or use it in the API calls but whatever I do, I cannot access that statement.
Putting the POST into a variable or function and calling it results in undefined.
This is what I'm trying to achieve:
import authOptions from "./credentials.js";
import pkg from "request";
const { post } = pkg;
const Spotify = {
getAccessToken() {
post(authOptions, function (error, response, body) {
if (!error && response.statusCode === 200) {
const token = body.access_token;
return token;
}
});
},
async search(input) {
const accessToken = Spotify.getAccessToken();
const response = await fetch(
`https://api.spotify.com/v1/search?q=${input}&type=artist`,
{
headers: {
"Authorization": `Bearer ${accessToken}`,
"Content-Type": "application/json",
},
}
);
const data = await response.json();
console.log(data);
},
};
export default Spotify;
Yet of course there's no Access Token returned from that post request.
Is there any way I can convert that piece of code into Async/Await?
You can create a new promise:
async function getAccessToken(){
return new Promise((resolve, reject) => {
post(authOptions, function (error, response, body) {
if(error){
reject(error);
} else if (response.statusCode === 200) {
const token = body.access_token;
resolve(token);
}
});
});
};
The resolve/reject allows you to return that value when the callback is called, and it passes the error to the caller to handle. And you can use this format to promisify any callback-based function, by calling it inside of a promise and using resolve/reject to return the value.

Body of request not being passed to API

I'm trying to do a PUT request to an update controller from a react form (Mongoose API). Everything is passing over to the request, except the body. Now, this is my first time using FormData, so I'm almost positive that is where the issue lies, but I can't seem to sort out where the problem is..
The Submit action from the form
const clickSubmit = () => {
// console.log('Values on submit before FormData: ', values) // Shows the state object as expected
let userData = new FormData()
values.name && userData.append('name', values.name)
values.email && userData.append('email', values.email)
values.password && userData.append('password', values.password)
values.about && userData.append('about', values.about)
values.photo && userData.append('photo', values.photo)
update({
userId: match.params.userId
}, {
t: jwt.token
}, userData).then((data) => {
if (data && data.error) {
setValues({...values, error: data.error})
} else {
setValues({...values, 'redirectToProfile': true})
}
})
}
The Helper Method that set up the request
const update = async (params, credentials, user) => {
console.log('The params: ', params) // passes the user ID just fine
console.log('The credentials:', credentials) // passes the JWT just fine
console.log('The user object: ', ...user) // has all the information I'm updating, albeit in an array form that I can't really work with
try {
let response = await fetch('/api/users/' + params.userId, {
method: 'PUT',
headers: {
'Accept': 'application/json',
'Authorization': 'Bearer ' + credentials.t
},
body: user
})
return await response.json()
} catch (err) {
console.log(err)
}
}
And the controller I've commented out the rest of the logic to remove the clutter while I TS this issue
const update = async (req, res) => {
console.log(req)
const user = await User.findById(req.params.userId)
console.log('user after find: ', user) // returns the user that I want to modify from the database
console.log('body of request: ', req.body) // empty object
}
UPDATE:
I was able to get the FormData into an actual object using Object.fromEntries(user) - but it still won't pass into the request.. I have tried two ways:
const update = async (params, credentials, user) => {
console.log('The params: ', params)
console.log('The credentials:', credentials)
console.log('The user object: ', ...user)
let infoToUpdate = Object.fromEntries(user)
console.log('infoToUpdate: ', infoToUpdate);
try {
let response = await fetch('/api/users/' + params.userId, {
method: 'PUT',
headers: {
'Accept': 'application/json',
'Authorization': 'Bearer ' + credentials.t
},
body: {
"name": infoToUpdate.name,
"email": infoToUpdate.email,
"about": infoToUpdate.about
}
})
return await response.json()
} catch (err) {
console.log(err)
}
}
And
const update = async (params, credentials, user) => {
console.log('The params: ', params)
console.log('The credentials:', credentials)
console.log('The user object: ', ...user)
let infoToUpdate = Object.fromEntries(user)
console.log('infoToUpdate: ', infoToUpdate);
try {
let response = await fetch('/api/users/' + params.userId, {
method: 'PUT',
headers: {
'Accept': 'application/json',
'Authorization': 'Bearer ' + credentials.t
},
body: infoToUpdate
})
return await response.json()
} catch (err) {
console.log(err)
}
}
But req.body is still an empty object..
This has been solved, and it was all my flippin computer's fault..
On a whim, I killed node_modules and the package.lock files and reinstalled the deps.. and it started working.. My guess is that bodyParser didn't fully install..
Thank you all for the help.

How to combine API GET request in nodejs?

I have several API Get request at once in nodejs. Each API have new data every couple minutes.
var express = require('express');
var router = express.Router();
var request = require("request");
let value1, value2, bodyData1, bodyData2;
var options = { method: 'GET',
url: 'https://api.example.com/data1',
qs:
{
valueType: 'MAXIMUM'
},
headers:
{
authorization: 'ABC123456',
accept: 'application/json; charset=utf-8' } };
request(options, function (error, response, body) {
if (error) throw new Error(error);
bodyData1 = JSON.parse(body);
value1 = bodyData1.value;
});
var options = { method: 'GET',
url: 'https://api.example.com/data2',
qs:
{
valueType: 'MAXIMUM'
},
headers:
{
authorization: 'ABC123456',
accept: 'application/json; charset=utf-8' } };
request(options, function (error, response, body) {
if (error) throw new Error(error);
bodyData2 = JSON.parse(body);
value2 = bodyData2.value;
});
router.get('/', function(req, res, next) {
res.render('home', {valueA : value1, valueB: value2});
});
module.exports = router;
I want to know if it is possible to combine them into one function?
Any other things I should concern?
It is possible if you have promises which is currently not the case. You have to wrap your request() call in a Promise. You can do it manually with a custom function requestToPromise.
You can then use Promise.all to call multiple promises in parallel.
function requestToPromise(options) {
return new Promise((resolve, reject) => {
request(options, (error, response, body) => {
if (error) return reject(error);
resolve(body);
});
});
}
var optionsRequest1 = {
method: "GET",
url: "https://api.example.com/data1",
qs: {
valueType: "MAXIMUM"
},
headers: {
authorization: "ABC123456",
accept: "application/json; charset=utf-8"
}
};
var optionsRequest2 = {
method: "GET",
url: "https://api.example.com/data2",
qs: {
valueType: "MAXIMUM"
},
headers: {
authorization: "ABC123456",
accept: "application/json; charset=utf-8"
}
};
var requestPromise1 = requestToPromise(optionsRequest1);
var requestPromise2 = requestToPromise(optionsRequest2);
Promise.all([requestPromise1, requestPromise2]).then(results => {
var [resultPromise1, resultPromise2] = results;
}).catch(error => {
//handle error
});
Instead of using the custom function requestToPromise you can also use util.promisify
const util = require('util');
const requestAsync = util.promisify(request);
Promise.all([requestAsync(optionsRequest1), requestAsync(optionsRequest2)]).then(results => {
var [resultPromise1, resultPromise2] = results;
}).catch(error => {
//handle error
});
You can use Redis cache to store data in memory for fast retrieval and fetch from memory very quickly.
Also, after some interval, you can add them to a database through bulk creation. It will decrease your database call.
// Example in sequilize
await db.table_name.bulkcreate([ {0bj1}, {obj2}..,{obj3 } ]);

Using request-promise to make an API call using jwt. [ERR_INVALID_ARG_TYPE] received

I'm learning nodejs and trying to make an API call. The API uses JWT to authenticate.
I created these functions to sign a token:
function token() {
const payload = {
iat: Math.floor(new Date() / 1000),
exp: Math.floor(new Date() / 1000) + 30,
sub: "api_key_jwt",
iss: "external",
jti: crypto.randomBytes(6).toString("hex")
};
return new Promise((resolve, reject) => {
jwt.sign(payload, privatekey, { algorithm: "RS256" }, function(
err,
token2
) {
if (err) reject(err);
else resolve(token2);
});
});
}
exports.genToken = async function() {
const header = {
"x-api-key": api
};
const data = {
kid: api,
jwt_token: await token()
};
async function authorization(req, res) {
try {
const auth = await rp({
url: authurl,
method: "POST",
headers: header,
body: data
});
res.send(auth.body);
} catch (error) {
res.send(404).send();
}
}
return {
"x-api-key": api,
Authorization: "Bearer " + authorization()
};
};
This works fine. Then I created a function to make the API call:
const token = require("./index").genToken;
const rp = require("request-promise");
exports.getOrderBook = function(res, error) {
const full_url = url + "order_book";
const auth = token();
rp({
url: full_url,
method: "GET",
headers: auth,
body: {
market: "btceur"
},
json: true
})
.then(function(response) {
res(response);
})
.catch(function(err) {
error(err);
});
};
And I call it using Express:
routes.get("/orderbook", async (req, res, next) => {
try {
const book = await orders.getOrderBook();
res.send(book);
} catch (error) {
next(error);
}
});
However, when I call my API, it shows an error in console:
TypeError [ERR_INVALID_ARG_TYPE]: The first argument must be one of
type string or Buffer. Received type object.
I guess the error is something with the token generation, because if I console.log(auth) in the getOrderBook function, it shows Promise { <pending> }, so probably an object is being passed as the jwt token.
Is it really the problem? I tried a lot of different solutions that I found on internet, however the concept of Async/Await is new to me, and I'm having some troubles to figure it out.
Thanks a lot in advance guys!
Since getToken is an anync function, the return is wrapped in a Promise as well so you would need another anync/await:
exports.getOrderBook = async function() {
let response;
try {
const full_url = url + "order_book";
const auth = await token();
response = await rp({
url: full_url,
method: "GET",
headers: auth,
body: {
market: "btceur"
},
json: true
});
} catch (e) {
// handle error
throw e
// or console.error(e)
}
return response;
};
In this line as well Authorization: "Bearer " + authorization(), authorization is returning a promise
const bearer = await authorization()
return {
"x-api-key": api,
Authorization: "Bearer " + bearer
};
For error handling wrap entire thing in try..catch block
exports.genToken = async function() {
try {
const header = {
"x-api-key": api
};
const data = {
kid: api,
jwt_token: await token()
};
async function authorization(req, res) {
let auth;
try {
auth = await rp({
url: authurl,
method: "POST",
headers: header,
body: data
});
// res object not available
// res.send(auth.body);
} catch (error) {
// res object not available, better throw error and handle in your middleware
// res.send(404).send();
}
return auth
}
const bearer = await authorization()
} catch (e) {
// handle error
}
return {
"x-api-key": api,
Authorization: "Bearer " + bearer
};
}

How to pass multiple values in node express options qs?

In my node.js api I am trying to pass multiple values into the fields tracking and carrier , but I have been unsuccessful. I am able to do this with single values (1 tracking number for tracking and 1 carrier code for carrier), but not for multiple values. I feel like there should be a loop involve, but maybe I am incorrect in stating this. Any help would be greatly appreciated.
Index.html
<script>
function UserParams() {
console.log('UserParams')
var trackingNumber = ['123', '456'];
var carrierCode = ['usps', 'fedex'];
var url = '/api/tracking/retrieve/';
$.ajax({
type: 'GET',
url: url,
data: { tracking: trackingNumber, carrier: carrierCode },
dataType: 'json',
error: function (e) {
console.log('Error Message: ', e.message);
}
});
}
</script>
Controller.js
app.get("/api/tracking/retrieve", (req, res) => {
var carrier = req.query.carrier;
var tracking = req.query.tracking;
console.log('carrier array', carrier);
console.log('tracking array', tracking);
var options = {
method: "GET",
url: 'https://api.example.com/v1/tracking',
qs: { carrier_code: carrier, tracking_number: tracking },
headers:
{
'api-key': process.env.SECRET_KEY_SE,
'accept': 'application/json'
}
}
console.log("Url: ", req.url);
console.log("Query: ", req.query);
res.send("ok");
request(options, function (error, response, body) {
if (!error && response.statusCode == 200) {
console.log('BODY', body)
var trackingData = JSON.parse(body)
table = 'tracking_table';
col = [
'user_id',
'tracking_number',
'carrier_code',
];
val = [
user_id,
trackingData.tracking_number,
options.qs.carrier_code,
];
main.create(table, col, val, function (data) {
res.json({
id: data.insertId,
user_id: user_id,
tracking_number: data.tracking_number,
carrier_code: data.carrier_code,
});
})
}
})
})
Result in terminal:
carrier array [ 'usps', 'fedex' ]
tracking array [ '123', '456' ]
Url: /api/tracking/retrieve/?tracking%5B%5D=123&tracking%5B%5D=456&carrier%5B%5D=usps&carrier%5B%5D=fedex
Query: { tracking: [ '123', '456' ],
carrier: [ 'usps', 'fedex' ] }
Edit: Edited question to include further context.
Depending on what the backend is you can try simply using arrays for this purpose, e.g.
var tracking = ['9361289691090998780245','784644233438'];
var carrier = ['usps','fedex'];
options = {
method: "GET",
url: 'https://api.example.com/v1/tracking',
qs: { tracking_number: tracking, carrier_code: carrier },
headers:
{
'api-key': process.env.SECRET_KEY_SE,
'accept': 'application/json'
}
}
request(options, function (error, response, body) {
if (error) console.error("An error occurred: ", error);
console.log("body: ", body);
});
If this is served by Express, the query string will be parsed correctly, e.g.
app.get("/api/tracking/retrieve", (req, res) => {
console.log("Url: ", req.url);
console.log("Query: ", req.query);
res.send("ok");
});
This is what would be logged by Express;
Url: /api/tracking/retrieve?tracking_number%5B0%5D=9361289691090998780245&tracking_number%5B1%5D=784644233438&carrier_code%5B0%5D=usps&carrier_code%5B1%5D=fedex
Query: { tracking_number: [ '9361289691090998780245', '784644233438' ], carrier_code: [ 'usps', 'fedex' ] }
Of course it depends on the API provider, so it might be worth seeing if they have documentation on how parameters should be formatted.
Try to change string tracking & carrier to array with function split
app.get('/api/tracking/retrieve', function (req, res) {
options = {
method: "GET",
url: 'https://api.example.com/v1/tracking',
qs: { tracking_number: tracking.split(','), carrier_code: carrier.split(',') },
headers:
{
'api-key': process.env.SECRET_KEY_SE,
'accept': 'application/json'
}
}
request(options, function (error, response, body) {
})
});

Categories