Anand Patel
Anand Patel

Reputation: 6421

How to Implement HTTP 'OPTIONS' verb for a REST API in Spring MVC?

How to implement HTTP 'OPTIONS' for a REST API in Spring MVC? Or can Spring framework discover by its own based on the controller definition? Are there use-cases for supporting 'OPTIONS' verb for the REST API? Is it common to support 'OPTIONS' for the REST API?

Thanks.

Upvotes: 1

Views: 5787

Answers (1)

CBIII
CBIII

Reputation: 865

I highly suggest reading up on CORS and the Same origin policy, which is pretty much the reason for OPTIONS method. The options method is used as a preflight request when the client is trying to make a non-simple http request to a different domain to determine if the client is allowed to or not. To see what is simple and what is non-simple look at the CORS spec. HTTP Options is common in REST APIs, because it is necessary for clients to make CROSS-Origin Resource Sharing. Sites from different domains can't easily make REST calls to each other due to the Same origin policy. Here is a link to handle the options method in Spring.

The options methods basically sends back to the client what the server can take and what domains are able to make calls to it. If your domain doesn't meet it's criteria than the Http request stops there. This preflight request is mandatory and is forced in most, if not all browsers and can't be turned off(at least easily).

Upvotes: 4

Related Questions