10

I've tried

axios.get(url, {headers:{},data:{}}) 

But it doesn't work with this.

6 Answers6

1

As far as I know you can't send body data with GET request. With get you can have only Headers. Just simply change to POST and then you can do something like this :

const bodyParameters = {
      key: "value",
    };
    const config = {
      headers: { Authorization: `Bearer ${userToken}` }, 
    };
      axios.post("http://localhost:5000/user", bodyParameters, config)
      .then((res)=> {
       console.log(res)
      })
      .catch((err) => console.log(err));
  };

or if you want to send headers with GET request

axios.get('/user', {
    params: {
      ID: 12345
    }
  })
  .then(function (response) {
    console.log(response);
  })
  .catch(function (error) {
    console.log(error);
  })
  .then(function () {
    // always executed
  });  
Ili
  • 267
  • 5
  • 10
1

// data is the data to be sent as the request body // Only applicable for request methods 'PUT', 'POST', 'DELETE , and 'PATCH'

https://stackoverflow.com/a/54008789

0

You should refer to https://github.com/axios/axios#request-config

Check the section for data and header.

Ayushya
  • 1,745
  • 1
  • 9
  • 14
  • 1
    unfortunately, data in GET method is not considered as body. apparently Axios doesn't support request body for GET method. weirdly, tools like Postman easily support it. I am also looking for a solution. – mostafa.S Nov 09 '21 at 06:40
0

You can try this:

const getData = async () => {
    try {
        const response = await axios.get(`https://jsonplaceholder.typicode.com/posts`, {
            method: 'GET',
            body: JSON.stringify({
                id: id,
                title: 'title is here',
                body: 'body is here',
                userId: 1
            }),
            headers: {
                "Content-type": "application/json; charset=UTF-8"
            }
        })
            .then(response => response.json())
            .then(json => console.log(json));
        console.warn(response.data);
    } catch (error) {
        console.warn(error);
    }
}
Khabir
  • 4,511
  • 1
  • 14
  • 28
0
  axios.get(
    BASEURL,
    {
        params: { user_id: userId },
        headers: {
          Authorization: `Bearer ${token}`,
          'Content-Type': 'application/json',
        },
    },
  );
PR7
  • 1,292
  • 1
  • 9
  • 23
  • 1
    Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Apr 01 '22 at 11:36
0

yeah, it's true it doesn't work to send body in Axios get even if it works in the postman or the backend.

  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Apr 15 '22 at 01:54