Jersey client is not setting the “origin” header for me and I wonder if I am missing anything.
String origin="http://www.localhost.com";
ClientResponse response= webResourceBuilder("my/endpoint")
.header( "origin" , origin)
.header("Access-Control-Request-Method", "POST")
.header("xorigin", origin)
.header("whatever", "test")
.accept("application/xml")
.get(ClientResponse.class);
When I inspect at runtime the request headers on the server side, I find “xorigin” and “whatever” headers, but not “origin” and “Access-Control-Request-Method”
How can I set these headers?
Default Jersey client uses HttpURLConnection to send requests to the server.
HttpUrlConnectionrestricts some headers to be sent in a request, see:You have two options how to handle this situation:
With the default Jersey client you need to set a system property
which suppresses removing restricted headers from the request.
Use ApacheHttpClient/ApacheHttpClient4 which doesn’t seem to have this restriction. Simply add one of the following dependencies to your project:
or
and then create your client like:
or