Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In HTTP logs I am seeing OPTIONS, GET, OPTIONS, POST why?

I am currently reviewing some proxy logs and seeing:

200 OPTIONS   /api/bob/
200 GET       /api/bob/
200 OPTIONS   /api/jim/
200 PUT       /api/jim/

I am wondering a couple of things, why is it doing the OPTIONS call before each request? And, my main question is: what are the benefits of doing so?

I would have thought this would add latency and an unnecessary overhead.

like image 596
Mr_road Avatar asked Aug 15 '14 09:08

Mr_road


People also ask

What is get and post in logs?

In summary, the GET requests are pages/data being requested by clients. POSTs are clients sending data to the server, usually expecting data as a response.

Why is there an options request before post?

Prevent sending the post data, if it wont be processed This is the only reason what is valid. Using options request will prevent sending the post data to the server unnecessarily.

Why does browser make options request?

This pre-flight request is made by some browsers as a safety measure to ensure that the request being done is trusted by the server. Meaning the server understands that the method, origin and headers being sent on the request are safe to act upon.

What is HTTP options used for?

The HTTP OPTIONS method requests permitted communication options for a given URL or server. A client can specify a URL with this method, or an asterisk ( * ) to refer to the entire server.


1 Answers

This might be CORS requests being made. See this MDN page on explanation how CORS works.

Basically, before making an actual request, client would make a OPTIONS request to kind of ask for permission to make an actual request. This is called a "preflight request".

One thing though - CORS doesn't require client to make an OPTIONS request before HTTP GET. So the client might be misbehaving.

You can verify whether the OPTIONS are caused by CORS by investigating their headers - if they do have Access-Control-Request-Method and Access-Control-Request-Headers headers, this is a preflight request and it's CORS.


Why preflight request is needed?

CORS is enforced by the browsers. By default most contemporary browsers wouldn't allow web JS code to make an AJAX request to the different server than this page is hosted on. This is a security measure.

CORS is a way for the browser (not the page itself!) to ask server whether it's safe to make an actual request.

For methods which could modify the resource on the server - for instance most POST's and all PUT methods - browser has to first ask whether it's okay to make this modifications. Server that supports CORS, will include special headers in the preflight response.

Without the preflight request: let's assume the browser makes the request to the server which does not support CORS. In that case making the request would probably modify the resource. And we don't want this!

For GET requests, which shouldn't change resource state, preflight request isn't necessary.

like image 146
kamituel Avatar answered Oct 01 '22 10:10

kamituel