Maxim Tkachenko
Maxim Tkachenko

Reputation: 5798

OPTIONS http method: how to implement it properly

I have an application with different resources:

  1. GET /report/555
  2. POST /messages

Now I want to support OPTIONS http method in the application. Should I implement one handler for all OPTIONS requests and return GET, POST in Allow header or it's better to return different responses for each route: only Allow: GET for /report/555 and only Allow: POST for /messages?

Upvotes: 1

Views: 911

Answers (2)

Rei
Rei

Reputation: 6363

Returning different responses is the correct choice here.

Here is the reasoning.

If you return Allow: GET, POST for both /report/555 and /messages, you are stating that POST is allowed for /report/555 which is incorrect and that GET is allowed for /messages which is also incorrect.

Therefore, you should return the correct response for each request target.

Using your example specifically, this means you should return Allow: GET for /report/555 and Allow: POST for /messages.

Upvotes: 1

Eric Stein
Eric Stein

Reputation: 13672

I'm not 100% sure I understand your question, but:

OPTIONS should be called on a specific target. It should generally return the verbs available for that target. If you want to find out the options for all endpoints, you need to send your request with a request-target of *.

For more information on OPTIONS, check out RFC 7231.

Upvotes: 1

Related Questions