70006

async GET request with body from browser

Question:

Ok, I know it's a bad idea and it shouldn't be done but for the sake of this question please assume there's no other way - I am given API endpoint that requires GET request with empty object as a body.

Is there a way to do async request from browser? I'm using axios library which uses XMLHttpRequest under the hood and <a href="https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/send" rel="nofollow">MDN</a> says that send wipes the body when HTTP method is GET. I tried using native fetch but it gives me this error in browser: TypeError: Failed to execute 'fetch' on 'Window': Request with GET/HEAD method cannot have body.

Is there any way to do it?

Answer1:

No, there isn't.

<a href="https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/GET" rel="nofollow">GET requests</a> can't have a request body, you can't make them have one. GET requests only retrieve data, they never send data.

An API that requires GET request with empty object as a body just doesn't work.

<h3>Edit:</h3>

Apparently, GET requests are allowed to have a body. Most implementations will ignore it or reject the request. But even if the server that provides your API allows a body, you can't use it:

From the spec about <a href="https://dvcs.w3.org/hg/xhr/raw-file/tip/Overview.html#the-send()-method" rel="nofollow">XMLHttpRequest#send</a>:

<blockquote>

Initiates the request. The optional argument provides the request entity body. <strong>The argument is ignored if request method is GET or HEAD.</strong> Throws an "InvalidStateError" exception if the state is not OPENED or if the send() flag is set.

</blockquote>

From the spec about the <a href="https://fetch.spec.whatwg.org/#request-class" rel="nofollow">Request class</a> in the fetch API:

<blockquote>

If either init’s body member is present and is non-null or <strong>inputBody is non-null</strong>, and <strong>request’s method is GET or HEAD</strong>, <strong>then throw a TypeError</strong>.

</blockquote>

That means that the answer to your question is still <em>No</em>.

Recommend

  • How to autoincrement id value using es6 js class notation?
  • Converting seconds to human readable format MM:SS Java [duplicate]
  • How to break v-for loop in vue.js?
  • ReactJS + React Router: How to reference a specific object to pass down as properties?
  • Output for loop to a file
  • Disable taphold default event, cross device
  • Sub-Matrix computations
  • MYSQLI prepared statement proceeds no output
  • How to access native iOS file system using Sencha touch 2 alone
  • Loss increases after restoring checkpoint
  • inserting data into multiple tables using php via a web form
  • Google app engine: how to handle concurrency (racing condition)
  • How to create a only mute/unmute button (like youtube) in html
  • Encoding special characters in xml
  • Binding producing 2 security tags , soap xml with 2 X509's+ usernmaetoken with nonce
  • Objects Sorting With date ,Time Problem in Array(Iphone Development)
  • Problems installing Common Lisp Sketch into Windows 10
  • how to reload page after uploading images
  • jqGrid Filter Toolbar and Date and Select filters
  • Eric5: The OK button of 'new project' dialog is disable
  • How to reduce a DAG by replacing each longest non-branching path by an edge connecting the start and
  • No Gradle file syntax highlighting in Eclipse Mars.2
  • Can't hide status bar in AVPlayerViewController's portrait mode
  • Is possible having two COM STA instances of the same component?
  • AWS RDS Parameter Group not changing MySQL encoding
  • Autocomplete source from project settings
  • How to check if a database and tables exist in sql server in a vb .net project?
  • Tensorflow Dataset API restore Iterator after completing one epoch
  • How to split wav file into two or more parts using c#
  • Content-Type alternative in MQTT
  • JavaScript Regex to Match Boundaries of Words with diacritics
  • How to specify generic type when the type is only known at runtime?
  • How to integrate angular2-material (alpha 8.2) with angular2-Quickstart app
  • How to handle div that is created dynamically in a table
  • How to encrypt Connectionstring written in web.config from codebehind?
  • Capture SIGFPE from SIMD instruction
  • Call Microservice from another Microservice within Docker
  • Android Library Projects on Windows and Mac