0

How i send body in method GET, but i'm not getting.

i try, but don't working

const res = await axios({
    method: 'get',
    url: `${this.state.url}Docto/Imagens`,
    headers: {
        Authorization: `Bearer ${Token}`,
        "Content-Type": "application/json"
    },
    processData: false,
    data: { Id: 4075 },
    body: { Id: 4075 }
})

In Postman, the request normally worked enter image description here

I know the correct would be to pass the parameter via querystring on get, but the back end did this way.

Rafael Augusto
  • 408
  • 9
  • 22
  • 2
    GET requests don't have a body. Any parameters that are part of the request are simply appended to the URL in a query string. – Robin Zigmond Apr 08 '19 at 13:01
  • GET requests are *allowed* to have a body, but servers are supposed to *ignore* it. https://stackoverflow.com/questions/978061/http-get-with-request-body –  Apr 08 '19 at 13:09

1 Answers1

-1

GET request does not have a body, use a query string instead. With axios I usually use npm package qs which is used also in axios docs:

Config:

    {
       ...
       // `paramsSerializer` is an optional function in charge of serializing `params`
      // (e.g. https://www.npmjs.com/package/qs, http://api.jquery.com/jquery.param/)
      paramsSerializer: function (params) {
        return Qs.stringify(params, {arrayFormat: 'brackets'})
      },
      ....
    }
Mosè Raguzzini
  • 12,776
  • 26
  • 36