1

In my test code, I need to send request using jersy client 2.7 in java. I need to set multiple headers of the web request. e.g.

Header1 12

Header2 abc

In my current working implementation the request is getting formed as mentioned below. Wherein I am only setting the Authorization token in header using function

public void setAuthorizationToken(String authorizationToken) {

        this.token = authorizationToken;

    }


response = webResource.request(MediaType.APPLICATION_JSON)                          .header("Authorization", this.token).post(Entity.entity(jsonString, mediaType));

I checked that there is a method headers (MultiValuedMap < String,Object > headers) to set multiple header KV pairs.

But did not get how to use it.

I have to set the header key value pair from some other function (say setHeader()) to add more headers in this request as mentioned above.

Can someone please let me know how can I do it?

Tim
  • 41,901
  • 18
  • 127
  • 145
user3763006
  • 11
  • 1
  • 2

1 Answers1

0
You can set header value like :


ClientResponse response = resource.type(MediaType.APPLICATION_JSON)
                    .header("headerKey1", headerValue1)
                    .header("headerKey2", headerValue2)
                    .header("headerKey3", headerValue3)
                    .post(ClientResponse.class, otherRequestData);


And in service :

String headerValue1= request.getHeader("headerKey1");
String headerValue2= request.getHeader("headerKey2");
String headerValue3= request.getHeader("headerKey3");
Dharmraj
  • 164
  • 1
  • 15
  • But the number of headers can be different in different requests. One request can have only auth token,another can have one or more header along with auth token.so I can not use .header as it is dynamic. – user3763006 Jun 23 '14 at 07:14
  • (http://stackoverflow.com/questions/8413608/sending-list-map-as-post-parameter-jersey) , it could help you if not please explain a little bit more the requirement. – Dharmraj Jun 23 '14 at 09:51