Axios get works with a URL string but ignores the object passed as the second argument
The URL typed into the browser returns data, but axios.get(url, { id: 5 }) returns the unfiltered listShort answer
axios.get takes a URL and an optional config object. It has no data argument, so an object passed second is read as config and its keys are ignored. Put query values under params in that config. Use axios.post, put or patch when you need a request body.
Fix it fast
1Find out which cause you have
Print the URL axios really requests. If the query string is missing there, the values never left the config object.
const res = await axios.get(url, { id: 5 }); console.log(axios.getUri(res.config));2Match what you see, then jump to its fix
- 1
The final URL has no ?key=value partQuery values were passed as the config, not under paramsFix - 2
The server receives no body and no fields, and the code uses data with getA request body was expected on a GET requestFix - 3
The same call shape worked with axios.post, but not after switching to axios.getpost-style arguments were used on getFix
- 1
What does the error look like?
The URL typed into the browser returns data, but axios.get(url, { id: 5 }) returns the unfiltered listThe server log shows a GET with no query stringRequest body is empty on a GET request
Cause 1 of 3
Query values were passed as the config, not under params
1Check
The signature is axios.get(url, config). Only known config keys are used, and params is the key that becomes the query string. A bare { id: 5 } is not a known key.
console.log(axios.getUri({ url, params: { id: 5 } }));2Fix
Wrap the values in params. If the URL already has a query string, axios merges the two.
const res = await axios.get('https://api.example.com/items', {
params: { id: 5, sort: 'name' },
});3Verify
The logged URL now ends with ?id=5&sort=name.
console.log(res.config.params, axios.getUri(res.config));Confirmed by axios.rest
Cause 2 of 3
A request body was expected on a GET request
1Check
The docs say data applies only to PUT, POST, DELETE and PATCH. For GET, the way to send values is params, which go into the query string.
grep -rn "axios.get(.*data" src/2Fix
Move the values to params for a GET. If the server really needs a body, the endpoint should be a POST, PUT or PATCH, and then the object is the second argument.
// GET: values in the query string
await axios.get('/api/search', { params: { q: 'shoes' } });
// POST: the second argument is the body
await axios.post('/api/search', { q: 'shoes' });3Verify
On the server, read req.query for the GET, and req.body for the POST.
curl -i "https://api.example.com/api/search?q=shoes"Confirmed by axios.rest
Cause 3 of 3
post-style arguments were used on get
1Check
post, put and patch are (url, data, config). get, delete, head and options are (url, config). Moving a call between them shifts what the second argument means.
grep -rn "axios\.\(get\|delete\|head\|options\)(" src/2Fix
For get and delete, pass one config object as the second argument and put the values under params. For delete with a body, set data inside that config.
await axios.delete('/api/items/5', { params: { force: true } });3Verify
Check the request in the network tab or print the built URL.
console.log(axios.getUri({ url: '/api/items/5', params: { force: true } }));Confirmed by axios.rest
How do I stop it coming back?
- Type the call with TypeScript generics so a wrong second argument is flagged by the editor.
- Wrap axios in one small api helper with params and data as named options.
- Log or test the built URL with axios.getUri for any new endpoint.
Sources
How was this page made?
I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.
Which stack is behind this page?
Projects of mine built on the same technologies:
- TaxiDexMy own multi-tenant taxi dispatch SaaS: API, dispatcher portal, driver and passenger apps, and the servers underneath.
- QistLockA NestJS and React SaaS driving an Android Device Owner agent that locks financed phones when payments lapse.
- WorkforceA field app, an admin console and a self-hosted Node.js server for push and reconciliation.
Related fixes
Web apps
Jest fails to parse a React Native or Expo dependency
Jest failed to parse a file. This happens e.g. when your code or its dependencies use non-standard JavaScript syntax
Jest does not run Babel on node_modules by default, and many React Native packages ship untranspiled source.
3 causes, each with a fixWeb apps
yarn is not recognized when running a React Native project
'yarn' is not recognized as an internal or external command, operable program or batch file.
The shell cannot find a yarn launcher on your PATH.
3 causes, each with a fixWeb apps
React shows Attempted import error when importing a component or function
Attempted import error:
The build found the file but not the name you asked for.
2 causes, each with a fixWeb apps
AxiosError Network Error with code ERR_NETWORK in a React app
Network Error
ERR_NETWORK means the request produced no readable response.
4 causes, each with a fixWeb apps
Can't resolve '../axios' in a React project
Module not found: Error: Can't resolve '../axios'
A path that starts with ./ or ../ is a file path, not the axios package.
3 causes, each with a fixWeb apps
Axios returns 401 from a React app but the login works elsewhere
Error: Request failed with status code 401
A 401 means the server received the request without valid credentials.
3 causes, each with a fix