Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Do the Fetch API and Axios see 304 responses as 200?

I don't know if it ever would, but if my server responded with HTTP status code 304, would the Fetch API (specifically response.ok) and axios.get() see the response as 200?

The documentation for both talk about the request being viewed as successful if the response code is in the range 200-299, but clearly 304 is outside this.

like image 477
Dave C Avatar asked Jun 14 '20 21:06

Dave C


People also ask

What is difference between Axios and fetch?

To send data, fetch() uses the body property for a post request to send data to the endpoint, while Axios uses the data property. The data in fetch() is transformed to a string using the JSON. stringify method.

Which of the following is the advantage of using Axios over fetch API?

Axios has the ability to intercept HTTP requests. Fetch, by default, doesn't provide a way to intercept requests. Axios has built-in support for download progress. Fetch does not support upload progress.

Does Axios use fetch or XMLHttpRequest?

Yes, Axios use XMLHttpRequest under the hood and jQuery. ajax() wraps XMLHttpRequest too. The native way to do request is with Fetch or directly with XMLHttpRequest/XmlHttpRequest2.


2 Answers

When a browser does a GET request with a If-Match or If-Modified-Since header, and the server responds with 304 Not Modified, the client will just see this as 200 OK.

The response is served from cache, instead of the server, and it allows the client (axios in your case) to not have to understand HTTP caching, but it can still take advantage of it.

I don't know what a client will do when they send a request without preconditions and still get a 304 response. A client wouldn't have an earlier cached response so this would definitely be 'broken'. I'd imagine you'd get an error but I'd be curious to see what.

like image 122
Evert Avatar answered Sep 21 '22 23:09

Evert


No, they will both not see the response as 200.

While axios.get will return a promise rejection, the return value of response.ok when using fetch API will be false and response.redirected will be true.

like image 39
Oluwafemi Sule Avatar answered Sep 21 '22 23:09

Oluwafemi Sule