255

I'm trying to send GET request as second parameter but it doesn't work while it does as url.

This works, $_GET['naam'] returns test:

export function saveScore(naam, score) {
  return function (dispatch) { 
    axios.get('http://****.nl/****/gebruikerOpslaan.php?naam=test')
      .then((response) => {
        dispatch({type: "SAVE_SCORE_SUCCESS", payload: response.data})
      })
      .catch((err) => {
        dispatch({type: "SAVE_SCORE_FAILURE", payload: err})
      })
  }
};

But when I try this, there is nothing in $_GET at all:

export function saveScore(naam, score) {
  return function (dispatch) { 
    axios.get('http://****.nl/****/gebruikerOpslaan.php',
    {
        password: 'pass',
        naam: naam,
        score: score
    })
      .then((response) => {
        dispatch({type: "SAVE_SCORE_SUCCESS", payload: response.data})
      })
      .catch((err) => {
        dispatch({type: "SAVE_SCORE_FAILURE", payload: err})
      })
  }
};

Why can't I do that? In the docs it clearly says it's possible. With $_POST it doesn't work either.

4 Answers 4

548

axios.get accepts a request config as the second parameter (not query string params).

You can use the params config option to set query string params as follows:

axios.get('/api', {
  params: {
    foo: 'bar'
  }
});
4
  • 14
    How do I extract it on the server side? Commented Jan 5, 2017 at 10:02
  • 1
    @zero_cool you dont need to access params , here wrt example you can access "foo" and it will return "bar" Commented Apr 27, 2017 at 12:22
  • Extracting on server side is important point here, I am sure you can use string foo as parameter for your method on server side, but I am not sure how to grab all parameters at once as object inside your server side method. any clue? I am trying to get this help from this url stackoverflow.com/questions/55602990/…
    – Kurkula
    Commented Apr 10, 2019 at 1:14
  • axios.get('api?&foo=bar') || axios.get(api?&${param}=${value})
    – king
    Commented Feb 8, 2021 at 2:27
171

On client:

axios.get('/api', {
  params: {
    foo: 'bar',
  },
});

On server:

function get(req, res, next) {
    
  let param = req.query.foo
  // ...
}
8
  • 2
    @danikorean, can we write the same client code without using request method alias, i.e. instead of axios.get using only axios({url:"url_goes_here",data:{params:{foo:'bar'}})
    – srbcheema1
    Commented Oct 21, 2019 at 23:32
  • 2
    This server code saved me hours, thank you! For anyone wondering, stick with 'params' for the .get call, and not 'body' as you may have seen while searching around. You can re-name it anything on the server side if you wish, but keep params for the client get.
    – DORRITO
    Commented Jan 17, 2020 at 15:44
  • 1
    axios.get('/api', { params}) !== axios.get('/api', params)
    – xgqfrms
    Commented Mar 11, 2020 at 10:13
  • Why is it that it is params on client, but query on server? Commented Oct 14, 2020 at 18:50
  • This saved me a lot of time, Static: axios.get('api?&foo=bar') || Dynamic: axios.get(api?&${param}=${value})
    – king
    Commented Feb 8, 2021 at 2:25
0

For me I was trying to send params using axios.post.

When switch both client and server to axios.get, req.query.foo worked like a charm

-2

This works fine for me. When it is post request nested data object isn't needed but in get requests, in order to send data, you need it.

axios.get('/api', {
    data: {
        foo: 'bar'
    }
}
2

Not the answer you're looking for? Browse other questions tagged or ask your own question.