1

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

Quentin
  • 857,932
  • 118
  • 1,152
  • 1,264
Jose A.
  • 131
  • 8
  • Does this answer your question? [Sending Request body for GET method in AXIOS throws error](https://stackoverflow.com/questions/54005520/sending-request-body-for-get-method-in-axios-throws-error) – Quentin Dec 22 '21 at 08:05

2 Answers2

1

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

Gabriele Petrioli
  • 183,160
  • 33
  • 252
  • 304
0

Try to send the data using the params property:

const { data: res } = await axios.get(`${baseUrl}/search`, {
    params: { date1, date2 },
    headers: { Authorization: `${tokenApp}` },
  });
lpizzinidev
  • 5,706
  • 1
  • 6
  • 19