{"id":2425,"date":"2022-08-30T15:24:45","date_gmt":"2022-08-30T15:24:45","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2014\/01\/12\/enabling-cors-for-cowboy-rest-api-collection-of-common-programming-errors\/"},"modified":"2022-08-30T15:24:45","modified_gmt":"2022-08-30T15:24:45","slug":"enabling-cors-for-cowboy-rest-api-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2022\/08\/30\/enabling-cors-for-cowboy-rest-api-collection-of-common-programming-errors\/","title":{"rendered":"Enabling CORS for Cowboy REST API-Collection of common programming errors"},"content":{"rendered":"<p>How can I enable CORS for cowboy rest handler? I tried to add options\/2 method, like this:<\/p>\n<pre><code>options(Req, State) -&gt;\n    {[\n       {, },\n       {, }\n     ], Req, State}.\n<\/code><\/pre>\n<p>but this causes errors like:<\/p>\n<pre><code>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...\n<\/code><\/pre>\n<p>Where is my mistake?<\/p>\n<ol>\n<li>\n<p>Cowboy documentation says you need to set header by using set_resp_headers, not return a list of headers:<\/p>\n<pre><code> %% If you need to add additional headers to the response at this point,\n %% you should do it directly in the options\/2 call using set_resp_headers.\n<\/code><\/pre>\n<p>So your code should look like:<\/p>\n<pre><code>options(Req, State) -&gt;\n    Req1 = cowboy_req:set_resp_header(, , Req),\n    Req2 = cowboy_req:set_resp_header(, , Req1),\n    {ok, Req2, State}.\n<\/code><\/pre>\n<p>You can test with<\/p>\n<pre><code>curl -H \"Origin: http:\/\/example.com\" \\\n  -H \"Access-Control-Request-Method: GET\" \\\n  -H \"Access-Control-Request-Headers: X-Requested-With\" \\\n  -X OPTIONS --verbose \\\nhttp:\/\/localhost:8080\n* About to connect() to localhost port 8080 (#0)\n*   Trying 127.0.0.1... connected\n* Connected to localhost (127.0.0.1) port 8080 (#0)\n&gt; OPTIONS \/ HTTP\/1.1\n&gt; User-Agent: curl\/7.21.4 (universal-apple-darwin11.0) libcurl\/7.21.4 OpenSSL\/0.9.8r     zlib\/1.2.5\n&gt; Host: localhost:8080\n&gt; Accept: *\/*\n&gt; Origin: http:\/\/example.com\n&gt; Access-Control-Request-Method: GET\n&gt; Access-Control-Request-Headers: X-Requested-With\n&gt;\n&lt; HTTP\/1.1 200 OK\n&lt; connection: keep-alive\n&lt; server: Cowboy\n&lt; date: Mon, 25 Mar 2013 15:59:11 GMT\n&lt; content-length: 0\n&lt; access-control-allow-methods: GET, OPTIONS\n&lt; access-control-allow-origin: *\n&lt;\n* Connection #0 to host localhost left intact\n* Closing connection #0\n<\/code><\/pre>\n<\/li>\n<\/ol>\n<p id=\"rop\"><small>Originally posted 2014-01-12 20:23:42. <\/small><\/p>","protected":false},"excerpt":{"rendered":"<p>How can I enable CORS for cowboy rest handler? I tried to add options\/2 method, like this: options(Req, State) -&gt; {[ {, }, {, } ], 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&#8230; Where is my mistake? Cowboy documentation says you need to set header by using set_resp_headers, [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-2425","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/2425","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=2425"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/2425\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=2425"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=2425"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=2425"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}