Here is the Format for RESTful call:
HEADERS:
Content-Type: application/json;charset=UTF-8
Authorization: Bearer Rc7JE8P7XUgSCPogjhdsVLMfITqQQrjg
REQUEST:
GET https://api.example.com/1/realTime?json={"selection":{"includeAlerts":"true","selectionType":"registered","selectionMatch":"","isTheEvent":"true","includeRuntime":"true"}}
Here is my codes:
try
{
Client client = Client.create();
WebResource webResource =
client.resource("https://api.example.com/1/realTime?json=
{"selection":{"includeAlerts":"true","selectionType":"registered","selectionMatch":"","isTheEvent":"true","includeRuntime":"true"}}");
//add header:Content-Type: application/json;charset=UTF-8
webResource.setProperty("Content-Type", "application/json;charset=UTF-8");
//add header: Authorization Bearer Rc7JE8P7XUgSCPogsdfdLMfITqQQrjg
value = "Bearer " + value;
webResource.setProperty("Authorization", value);
MultivaluedMap<String, String> queryParams = new MultivaluedMapImpl();
queryParams.add("json", js);
//Get response from RESTful Server
jsonStr = webResource.get(String.class);
System.out.println("Testing:");
System.out.println(jsonStr);
}
catch (Exception e)
{
System.out.println (e.getMessage());
e.printStackTrace();
System.exit(0);
}
But it returns error
com.sun.jersey.api.client.UniformInterfaceException: GET https://api.example.com/1/realTime? returned a response status of 500
at com.sun.jersey.api.client.WebResource.handle(WebResource.java:607)
at com.sun.jersey.api.client.WebResource.get(WebResource.java:187)
at net.yorkland.restful.GetThermostatlist.GetThermostats(GetThermostatlist.java:60)
I think I didn't add headers correctly.
Can someone help me to fix it? Please give me advice how to add headers on request.
Thank you
The most straightforward way to define a header is calling WebTarget#request to obtain an Invocation. Builder that provides the header method.
You can pass duplicate headers as well and there will not be any overwritten of values. For example, If we pass two values of header1 as value1 and value2 then it will be merged and will be passed as header1=value1 and header1=value2. It is the default behaviour.
Jersey is an open source framework for developing RESTFul Web Services. It also has great inbuilt client capabilities.
I use the header(name, value) method and give the return to webResource var:
Client client = Client.create();
WebResource webResource = client.resource("uri");
MultivaluedMap<String, String> queryParams = new MultivaluedMapImpl();
queryParams.add("json", js); //set parametes for request
appKey = "Bearer " + appKey; // appKey is unique number
//Get response from RESTful Server get(ClientResponse.class);
ClientResponse response = webResource.queryParams(queryParams)
.header("Content-Type", "application/json;charset=UTF-8")
.header("Authorization", appKey)
.get(ClientResponse.class);
String jsonStr = response.getEntity(String.class);
I think you're looking for header(name,value) method. See WebResource.header(String, Object)
Note it returns a Builder though, so you need to save the output in your webResource var.
Try this!
Client client = ClientBuilder.newClient();
String jsonStr = client
.target("http:....")
.request(MediaType.APPLICATION_JSON)
.header("WM_SVC.NAME", "RegistryService")
.header("WM_QOS.CORRELATION_ID", "d1f0c0d2-2cf4-497b-b630-06d609d987b0")
.get(String.class);
P.S You can add any number of headers like this!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With