As I try to send a request to my REST endpoint, the following code is executed:
this.http.request(path, requestOptions);
The path
is set to:
http://localhost:8082/commty/cmng/users
and the requestOptions
are as follows:
{
headers: { user: "sdf", password: "sdf" }
}
An issue arises when the browser attempts to obtain CORS permission before sending the request.
Here's the CURL request the browser makes for CORS HTTP:
curl "http://localhost:8082/commty/cmng/users" -X OPTIONS
-H "Host: localhost:8082"
-H "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"
-H "Access-Control-Request-Method: PUT"
-H "Access-Control-Request-Headers: passwd,user"
-H "Origin: http://localhost:3000"
-H "Connection: keep-alive"
Although a 200-OK
response is received after the CORS request, the flow gets interrupted, preventing my request from being sent.
The response content is shown below:
Object { _body: error, status: 0, ok: false, statusText: "", headers: Object, type: 3, url: null }
This is the CORSFilter.java
implementation:
@Provider
public class CORSFilter implements ContainerResponseFilter
{
@Override
public void filter(ContainerRequestContext requestContext, ContainerResponseContext responseContext) throws IOException
{
responseContext.getHeaders().add("Access-Control-Allow-Origin", "*");
responseContext.getHeaders().add("Access-Control-Allow-Headers", "origin, content-type, accept, authorization");
responseContext.getHeaders().add("Access-Control-Allow-Credentials", "true");
responseContext.getHeaders().add("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS, HEAD");
responseContext.getHeaders().add("Access-Control-Max-Age", "1209600");
}
}
However, the server responds with the following:
Object {
_body: error,
status: 0,
ok: false,
statusText: "",
headers: Object,
type: 3,
url: null
}
https://i.sstatic.net/MyRzG.png
Error Message
Object {
_body: error,
status: 0,
ok: false,
statusText: "",
headers: Object,
type: 3,
url: null
}
Any suggestions on resolving this issue?