Append a query param to a GET request?
After looking at Axios' README, it looks like the second argument needs the key params
. You can try:
app.get('/api/posts', async (req, res, next) => { try { const url = 'https://myurl.com/blah/blah'; const options = { params: { tag: req.query.tag } }; const response = await axios.get(url, options); res.json(response.data); } catch (err) { // Be sure to call next() if you aren't handling the error. next(err); }});
If the above method does not work, you can look into query-string
.
const querystring = require('query-string');app.get('/api/posts', async (req, res, next) => { try { const url = 'https://myurl.com/blah/blah?' + querystring.stringify({ tag: req.params.tag }); const response = await axios.get(url); res.json(response.data); } catch (err) { next(err); }});
Responding to your comment, yes, you can combine multiple Axios responses. For example, if I am expecting an object literal to be my response.data
, I can do:
const response1 = await axios.get(url1)const response2 = await axios.get(url2)const response3 = await axios.get(url3)const combined = [ { ...response1.data }, { ...response2.data }, { ...response3.data }]