How to respond to OPTIONS HTTP Method in rails-api?
Asked Answered
A

1

16

I'm using rails-api to build a public json api.

I would like to respond to OPTIONS HTTP Method to take advantages of Cross-Origin Resource Sharing.

http://www.w3.org/TR/cors/

I'm doing this:

headers['Access-Control-Allow-Methods'] = 'POST, PUT, DELETE, GET, OPTIONS'

It works, but some URLs does not support all HTTP methods. In those cases I'm lying.

I don't want to configure Access-Control-Allow-Methods for each URL.

Is there a way to respond Access-Control-Allow-Methods based on my routes?

Append answered 15/11, 2013 at 23:41 Comment(0)
P
11

check out the rack-cors gem, it allows you to configure it like a routes file

https://github.com/cyu/rack-cors

Patmos answered 10/12, 2013 at 16:52 Comment(1)
rack-cors is very nice but it is not based on my routes. If my routes responds to GET /list with 200 and POST /list with 404, then I want it to responds OPTIONS with Access-Control-Allow-Method: GET, OPTIONS only. Rack-cors do that, but I have to configure. I don't want to configure it again since I have already configured on routes file. However I will accept your answer because I think this way better now.Append

© 2022 - 2024 — McMap. All rights reserved.