Enabling CORS for Cowboy REST API-Collection of common programming errors

How can I enable CORS for cowboy rest handler? I tried to add options/2 method, like this:

options(Req, State) ->
    {[
       {, },
       {, }
     ], Req, State}.

but this causes errors like:

Error in process  with exit value: {{case_clause,{[{,},{,}],{http_req,#Port,ranch_tcp,keepalive,,,{1,1},{{127,0,0,1},56522},,undefined,9090,,undefined,,undefined,,[],[{,},{,},{,},{,},{,},{,},{,},{,},{,}],[{,[]}],undefined,[],waiting,undefined,,false,waiting,[],,undefined},undefined...

Where is my mistake?

  1. Cowboy documentation says you need to set header by using set_resp_headers, not return a list of headers:

     %% If you need to add additional headers to the response at this point,
     %% you should do it directly in the options/2 call using set_resp_headers.
    

    So your code should look like:

    options(Req, State) ->
        Req1 = cowboy_req:set_resp_header(, , Req),
        Req2 = cowboy_req:set_resp_header(, , Req1),
        {ok, Req2, State}.
    

    You can test with

    curl -H "Origin: http://example.com" \
      -H "Access-Control-Request-Method: GET" \
      -H "Access-Control-Request-Headers: X-Requested-With" \
      -X OPTIONS --verbose \
    http://localhost:8080
    * About to connect() to localhost port 8080 (#0)
    *   Trying 127.0.0.1... connected
    * Connected to localhost (127.0.0.1) port 8080 (#0)
    > OPTIONS / HTTP/1.1
    > User-Agent: curl/7.21.4 (universal-apple-darwin11.0) libcurl/7.21.4 OpenSSL/0.9.8r     zlib/1.2.5
    > Host: localhost:8080
    > Accept: */*
    > Origin: http://example.com
    > Access-Control-Request-Method: GET
    > Access-Control-Request-Headers: X-Requested-With
    >
    < HTTP/1.1 200 OK
    < connection: keep-alive
    < server: Cowboy
    < date: Mon, 25 Mar 2013 15:59:11 GMT
    < content-length: 0
    < access-control-allow-methods: GET, OPTIONS
    < access-control-allow-origin: *
    <
    * Connection #0 to host localhost left intact
    * Closing connection #0