0

I have seen so many threads on stackoverflow with this question, only for the answers to fizzle out into something completely different to what is being asked. I know its bad practice or not standard practice to submit body with a GET request.

But this is the situation. I have an api, that takes custom headers for authorization as well as accepted data type.

The same api instead of accepting the 'productId' as a parameter as in all humanly built APIs that uses GET, this one takes the 'productId' as a json submitted in the body of the API.

{
'productId':'297552891'
}

I have tested it out in postman and it works. How can i do this with jQuery or ajax or anything javascripty from the web page?

Thanks

Neo
  • 143
  • 12
  • GET requests do not have bodies. Why exactly is it so important to circumvent web standards and go contrary to what the protocols are expected to do? – Taplar Sep 15 '20 at 21:44
  • @Taplar It does. And postman successfully submitted a body with my request. And am not doing this by choice. i dont have a choice. I gotta consume this api for our thing to work :/ For ref: https://stackoverflow.com/questions/978061/http-get-with-request-body – Neo Sep 15 '20 at 21:46
  • Can you provide a link to the docs for this api? – charlietfl Sep 15 '20 at 22:07
  • @charlietfl this is an in house application that is run within an enterprise. Unfortunately no documentation is available. It takes "Access-Key" and "Content-Type" in header with the token and "application/json" as its values respectively. Also in postman under the Body tab, we set raw and JSON from the dropdown to send the json shown in the question. – Neo Sep 15 '20 at 22:12
  • OK but according to MDN docs neither fetch() or XMLHttpRequest support a body for GET and will make it null. What happens when you send as POST? – charlietfl Sep 15 '20 at 22:13
  • 1
    "Why can you not send it as a POST?" is more the question. – Taplar Sep 15 '20 at 22:14
  • @Taplar the api doesn't accept a POST request. It only accepts a GET request. I tried it in postman. – Neo Sep 15 '20 at 22:15
  • 1
    Then change the API. – Taplar Sep 15 '20 at 22:15
  • @charlietfl according to the link I posted above it looks like its supported but it's not standard practice. Ref: https://stackoverflow.com/questions/978061/http-get-with-request-body – Neo Sep 15 '20 at 22:16
  • @Taplar my man with the most lamest answers :P. This is the predicament I am in and I would've changed it had i got the chance, but here we're. – Neo Sep 15 '20 at 22:17
  • 1
    Well if the MDN docs are accurate regarding fetch() and XMLHttpRequest then you would probably need to use a server side proxy that can send the GET with a body. Can we assume you have tried using both and if so what did the body look like? – charlietfl Sep 15 '20 at 22:20
  • @charlietfl I have tried only using jquery ajax. And postman to test if the api worked. Haven't tried with fetch(). I tried with XMLHttpRequest, but when i checked with fiddler, the request didn't send any body. – Neo Sep 15 '20 at 22:30
  • 1
    So that suggests the mdn docs are accurate then. jQuery ajax just wraps XMLHttpRequest ( and possibly fetch() in newer versions??). It has no proprietary transport mechanism not provided by the browser. Would be the same for other libs like axios, superagent etc – charlietfl Sep 15 '20 at 22:31

0 Answers0