Home > Net >  axios get request with body and header
axios get request with body and header

Time:12-22

how can I something like this, sending body params and header with Authorization token into this

enter image description here

const searchByDate = async ({ date1, date2 }) => {
  const tokenApp = window.localStorage.getItem('token');
  const { data: res } = await axios.get(`${baseUrl}/search`, {
    data: { date1: date1, date2: date2 },
    headers: { Authorization: `${tokenApp}` },
  });
  return res;
};

so far it is throwing me an error Required request body is missing

CodePudding user response:

Try to send the data using the params property:

const { data: res } = await axios.get(`${baseUrl}/search`, {
    params: { date1, date2 },
    headers: { Authorization: `${tokenApp}` },
  });

CodePudding user response:

In general there is no point in a body for GET requests, so axios does not support it.

If you read the axios config documentation, you will find

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

You can read more at HTTP GET with request body for the reasons.


If you want to send data in a GET request use the params property

// params are the URL parameters to be sent with the request
// Must be a plain object or a URLSearchParams object

  • Related