I have to connect to a poorly implemented server that only understands Content-Type (capital-T) and not Content-type. How can I ask my JAX-WS client to send Content-Type?
I’ve tried:
Map<String, List<String>> headers = (Map<String, List<String>>)
((BindingProvider)port).getRequestContext().get(MessageContext.HTTP_REQUEST_HEADERS);
But headers is null. What am I doing wrong?
I’ve dug this question a bit more and, sadly, I’m afraid the answer is: you can’t. Let me share my findings.
First, the code that you’ll find in https://jax-ws.dev.java.net/guide/HTTP_headers.html does not give you access to the HTTP headers of the future HTTP request (that hasn’t been created at this point), it allows you to set additional HTTP headers for making a request (that will be added to the HTTP request later).
So, don’t expect the following code to not return
nullif you don’tputanything before (and actually, you’ll only get what youputin there):Then, I did a little test based on the code provided in the same link:
And this is what I see in the HTTP request when running the client code:
Do you notice the difference: only the first char of the
X-Client-Versionheader is kept upper cased, the rest is lowered!And indeed, if you check the class
c.s.x.w.t.Headersthat is used to represent HTTP request (and response) headers, you’ll see that it “normalizes” keys when they are added (innormalize(String)):So, while the
c.s.x.w.t.h.c.HttpTransportPipeclass (my understanding is that this is where the HTTP request is created, this is also where previously added headers will be added to the HTTP request headers) actually adds"Content-Type"as key in ac.s.x.w.t.Headersinstance, the key will be modified because of the previously mentioned implementation detail.I may be wrong but I don’t see how this could be changed without patching the code. And the odd part is that I don’t think that this “normalizing” stuff is really RFCs compliant (didn’t check what RFCs say about headers case though). I’m surprised. Actually, you should raise an issue.
So I see three options here (since waiting for a fix might not be an option):