I am trying to add multiple header. But failed miserably so far. I had tried lots of code tweaking but failed. Can someone help me fix the code or at least tell me what's wrong ?
Header mapping code:
Map<String, String> headers = new HashMap<String, String>();
headers.put("authorization", authToken);
headers.put("API-Version", apiVersion);
headers.put("Content-Type", MediaType.APPLICATION_JSON);
actual calling code:
String serviceUrl = serviceHostUrl;
Client client = Client.create();
WebResource webResource = client.resource(serviceUrl).path(path);
WebResource.Builder builder = webResource.getRequestBuilder();
if(headers != null && !headers.isEmpty()) {
for(Map.Entry<String, String> entry : headers.entrySet()) {
builder.header(entry.getKey(), entry.getValue());
}
}
ClientResponse response = builder.post(ClientResponse.class, input);
UPDATE
if in second snippet I use below code instead of setting headers in loop, it works fine. That's really weird.
builder.header("authorization", "Basic SDFSFSDFSDFSDFSDFSDFSDF");
builder.header("API-Version", "5.2");
builder.header("Content-Type", MediaType.APPLICATION_JSON);
I think the issue here is, type of MAP that you are trying to access.
Map<String, Object> headers = new HashMap<String, Object>();
The WebSource builder accepts header(String,Object). So Try with changing the map type.
Basic auth is like: Authorization = "Authorization" ":" credentials
an example
byte[] loginBytes = ("1" + ":" + "1").getBytes();
StringBuilder authString = new StringBuilder().append("Basic ")
.append(Base64.encodeToString(loginBytes, Base64.NO_WRAP));
_headers.put("Authorization", authString );
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.