1

I'm doing some POST requests from my angular js app to my RESTful API implemented using RestEasy.
The case is that I need CORS so I added a servlet filter with this code:

public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException {
    HttpServletResponse response = (HttpServletResponse) res;
    response.addHeader("Access-Control-Allow-Origin", "*");
    response.addHeader("Access-Control-Allow-Methods", "POST, GET, OPTIONS, DELETE");
    response.addHeader("Access-Control-Max-Age", "3600");
    response.addHeader("Access-Control-Allow-Headers", "Content-Type");
    chain.doFilter(req, res);
}

But I can't figure out why it works only with GET requests and not POST requests, the error on chrome's console is:

No 'Access-Control-Allow-Origin' header is present on the requested resource

My POST request is:

$http({method: 'POST', 
       url: myUrl,
       data: $scope.data,
       headers: {'Content-Type': 'application/json'}
});  

This is the reponse I receive on POST:

Allow:POST, OPTIONS
Content-Length:0
Date:Thu, 03 Apr 2014 23:27:22 GMT
Server:Apache-Coyote/1.1

Any Idea? Thanks!
EDIT:
Tested on IE10 and it works but doesn't work on chrome neither firefox ... any body knows why?

Adnane.T
  • 280
  • 1
  • 4
  • 13
  • how are you requesting with GET? – Eliran Malka Apr 03 '14 at 23:14
  • the same way but with 'GET' instead of 'POST' and another url but on the same WS – Adnane.T Apr 03 '14 at 23:24
  • i don't think you need to explicitly allow the `Content-Type` header. try and remove that. – Eliran Malka Apr 03 '14 at 23:47
  • still doesn't work, as you see in my edit, it seems that its browser thing ... FF and chrome doesn't receive the same response headers as IE – Adnane.T Apr 03 '14 at 23:48
  • works on IE but not on Chrome or Firefox? that's [odd](http://caniuse.com/#search=CORS). – Eliran Malka Apr 04 '14 at 00:03
  • also, try and change the `Content-Type` header on the request to `text/plain`. – Eliran Malka Apr 04 '14 at 00:05
  • one more guess: use `addHeader` instead of `setHeader` – Eliran Malka Apr 04 '14 at 00:07
  • yes, searching a little I found out that FF and chrome change the request headers and use the called "preflight requests", I can't put it on text/plain because my POST consumes directly JSON to map it – Adnane.T Apr 04 '14 at 00:08
  • it's a hack, of course, but you can still use `text/plain` if you serialize the payload just before sending it, e.g. `angular.toJson($scope.data)`. – Eliran Malka Apr 04 '14 at 00:14
  • in order to use the @Consumes annotations you need to send the correct data type not a plain/text representing you json – Adnane.T Apr 04 '14 at 00:33
  • uhm, *what `@Consumes` annotations*? you haven't posted any servlet code... – Eliran Malka Apr 04 '14 at 00:36
  • google it, it's part of the JEE spec – Adnane.T Apr 04 '14 at 00:36
  • right, i'm asking (in my serpentine way), where's that code of yours? how did you use those annotations? – Eliran Malka Apr 04 '14 at 00:38
  • i don't need to post it since my problem is whith allowing preflight requests, IE creates a POST request and it works perfectly, while Chrome and FF creates an OPTIONS encapsulating the POST and I don't know how to make it work – Adnane.T Apr 04 '14 at 00:40
  • the only way i see possible would be using another content type suitable for simple request, and consuming such type in the server, thus avoiding the trigger of preflight requests. hope i'm proved wrong, it sounds unlikely that this is the only option.... – Eliran Malka Apr 04 '14 at 00:43
  • Are you sending credentials with your request? `app.config(function($httpProvider) { $httpProvider.defaults.withCredentials = true; }` – sjdaws Apr 04 '14 at 05:07

2 Answers2

1

Well finally I came to this workaround:
The reason it worked with IE is because IE sends directly a POST instead of first a preflight request to ask for permission.
But I still don't know why the filter wasn't able to manage an OPTIONS request and sends by default headers that aren't described in the filter (seems like an override for that only case ... maybe a restEasy thing ...)

So I created an OPTIONS path in my rest service that rewrites the reponse and includes the headers in the response using response header

I'm still looking for the clean way to do it if anybody faced this before.

Adnane.T
  • 280
  • 1
  • 4
  • 13
1

I have had good luck configuring Cross-origin resource sharing (CORS) for my API (on Wildfly) by using this lib:

<dependency>
<groupId>com.thetransactioncompany</groupId>
<artifactId>cors-filter</artifactId>
<version>2.1</version>
</dependency>

It's very easy to setup. Just add the above dependency to your pom and then add the following config to the webapp section of your web.xml file.

<filter>
    <filter-name>CORS</filter-name>
    <filter-class>com.thetransactioncompany.cors.CORSFilter</filter-class>

    <init-param>
        <param-name>cors.allowGenericHttpRequests</param-name>
        <param-value>true</param-value>
    </init-param>

    <init-param>
        <param-name>cors.allowOrigin</param-name>
        <param-value>*</param-value>
    </init-param>

    <init-param>
        <param-name>cors.allowSubdomains</param-name>
        <param-value>false</param-value>
    </init-param>

    <init-param>
        <param-name>cors.supportedMethods</param-name>
        <param-value>GET, HEAD, POST, DELETE, OPTIONS</param-value>
    </init-param>

    <init-param>
        <param-name>cors.supportedHeaders</param-name>
        <param-value>*</param-value>
    </init-param>

    <init-param>
        <param-name>cors.supportsCredentials</param-name>
        <param-value>true</param-value>
    </init-param>

    <init-param>
        <param-name>cors.maxAge</param-name>
        <param-value>3600</param-value>
    </init-param>

</filter>

<filter-mapping>
    <!-- CORS Filter mapping -->
    <filter-name>CORS</filter-name>
    <url-pattern>*</url-pattern>
</filter-mapping>

You can also configure it with a properties file instead if you prefer. This lib works like a charm and gives you a lot of configuration flexibility!

Alex Petty
  • 423
  • 3
  • 6