Sending an Array of Values in a GET Request

02-15-2022

Sometimes you might need to send multiple values for a given parameter in a GET request to your API. For example, you have a GET /users API endpoint and you want to lookup information on several users at once. You might want your query to look something like: /users?username=bob;sally;john. This can work well for most data by using a delimiter like the ;.

In Express, our GET /users endpoint might look like:

1
2
3
4
5
6
7
8
9
10
const express = require('express');
const app = express();

...

app.get('/users', (req,res)=>{
const usernames = req.query.username.split(';');
console.log(usernames); // ['bob', 'sally', 'john']
res.send('Found users');
})

This quick and simple option can be extended for more use cases, but this meets the needs of most APIs.