Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Next.js and React

Axios get works with a URL string but ignores the object passed as the second argument

ErrorThe URL typed into the browser returns data, but axios.get(url, { id: 5 }) returns the unfiltered list

Short 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.

  • Applies to axios 1.x, React Native 0.7x-0.8x, Node.js 22
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 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));
  2. 2Match what you see, then jump to its fix

    1. 1The final URL has no ?key=value partQuery values were passed as the config, not under paramsFix
    2. 2The server receives no body and no fields, and the code uses data with getA request body was expected on a GET requestFix
    3. 3The same call shape worked with axios.post, but not after switching to axios.getpost-style arguments were used on getFix

What does the error look like?

  • The URL typed into the browser returns data, but axios.get(url, { id: 5 }) returns the unfiltered list
  • The server log shows a GET with no query string
  • Request 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

Did this fix it?

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

Did this fix it?

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

Did this fix it?

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:

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 fix

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.