-1

I have to pass the parameters to the body and not the query. I tried it like this.

const fetch=async()=>{
  let res =  await
    apiCall("URL" + {
      "companyArr":["SBI Life Insurance Company Ltd.","NTPC Ltd"],
      "secodeArr":["NSE","NSE"],
      "startDate":"Jul 1, 2022",
      "endDate":"Jul 13, 2022"
  },
  "GET",null)

console.log(res);
if (res.status === 200) {
    console.log(res.data.data);
}
  }

But I am getting 'Request failed with status code 404' error message. What is the correct way to go about this?

Christian Baumann
  • 3,188
  • 3
  • 20
  • 37
user460567
  • 133
  • 9
  • 1
    Does this answer your question? [HTTP GET with request body](https://stackoverflow.com/questions/978061/http-get-with-request-body) – mbojko Jul 19 '22 at 14:50
  • Can you explain why you want to use form data on a GET request. Although technically possible some servers will reject the request, and it's very unconventional to say the least. – bitoiu Jul 19 '22 at 15:03
  • @mbojko not really, they're advising to not pass to the body but I have to do it – user460567 Jul 19 '22 at 15:12
  • @bitoiu I was asked to pass to the body by the person who wrote the API, don't really have a choice in the matter – user460567 Jul 19 '22 at 15:13
  • Request failed with status code 404: don't think this would be a problem with the body. I would expect a 5xx error from that type of error. – bitoiu Jul 19 '22 at 17:11
  • _"But I am getting 'Request failed with status code 404' error message."_ - well of course you do. A simple `console.log("URL" + {...})` would have shown you, that the value you are creating there, is `URL[object Object]` - no wonder, that 404s. Why did you even try to add anything to the URL - when your question explicitly is about sending data in the request body? – CBroe Jul 20 '22 at 09:10
  • https://stackoverflow.com/a/61653814/1427878: _"XMLHttpRequest (the underlying API used by jQuery Ajax) does not support the sending of a request body with a GET request (**no[r] does XHR's successor — fetch**)."_ - highlight by me. You might need to find a different way to make the request altogether then. Or ask the person who's in charge of this API to stop violating HTTP :-) – CBroe Jul 20 '22 at 09:15

1 Answers1

1

If you really need to send information in a GET call then you should probably look into using query parameters

Check this thread for more information: setting-query-string-using-fetch-get-request

Joacim Norbeck
  • 203
  • 1
  • 6