jersey client get error message body - java

I am using the following code to consume a rest post service
Client client = ClientBuilder.newClient();
WebTarget target = client.target("wrong url");
Invocation.Builder builder = target.request(MediaType.APPLICATION_JSON);
Response response = builder.post(Entity.entity(param, MediaType.APPLICATION_JSON), Response.class);
As expected I am getting error. and status code 400 Bad Request. But i am not getting the error message. when i run response.getStatusInfo() i get bad request but my server sends additional info.
when i call this using postman i get error info in the Body window.
So how do I get this error body info from the response object?
or any other way???

You get the response body as usual with Response#readEntity:
Read the message entity input stream as an instance of specified Java type using a MessageBodyReader that supports mapping the message entity stream onto the requested type.
Example:
Client client = ClientBuilder.newClient();
WebTarget target = client.target("wrong url");
Invocation.Builder builder = target.request(MediaType.APPLICATION_JSON);
Response response = builder.post(Entity.entity(param, MediaType.APPLICATION_JSON), Response.class);
String body = response.readEntity(String.class);
With Response#getStatusInfo you get only HTTP status, class and reason phrase.

I am new to Jersey and JAX-RS, but I believe this question is equivalent to Handling custom error response in JAX-RS 2.0 client library, which has much better answers.

Related

Why 400 error code in POST call using Jersey client

I am calling a post request using Jersey rest client which contains no request body but contains authorization header. I am always getting 400 error. I tried from postman i got 200 response. Here is my code
ClientResponse response = null;
Client cliente=Client.create();
cliente.addFilter(new LoggingFilter(System.out));
WebResource webResource=cliente.resource("https://URL/token");
Builder builder = webResource.accept("application/json");
builder.type(MediaType.APPLICATION_JSON);
builder.header("Authorization", "Basic YbjjkwliOTQtNjRiYy00NWE5LWFhMzQtMTFhNDkyZZjNTVlOjZjYjk2OTMwNGE5YTQ3OTlhODVjZTM5MDFiMDEyMTI2";
builder.post(ClientResponse.class,Entity.json(""));
Don't use the way you are trying right now. Use HttpAuthenticationFeature from Jersey like this
HttpAuthenticationFeature feature = HttpAuthenticationFeature.basic("username", "password");
 
final Client client = ClientBuilder.newClient();
client.register(feature);
If you are getting 200 success respone in postman and failure response in your application then you need to check your api with another api tester online tool. I had the same issue i put / at the last of end point.

Cannot get data from Elastic 6.0: “error”:”Content-Type header [text/plain] is not supported”,”status”:406

I create a client in java and want to get the query results from index but got the 406 error:
"Content-Type header [text/plain] is not supported","status":406.
Java platform: 1.6
ES version: 6.0
Below is the code snippet:
Client clientInstance = Client.create();
clientInstance.addFilter(new HTTPBasicAuthFilter("", ""));
WebResource webResource =
clientInstance.resource("http://hostname:9200/indexname/_search");
ClientResponse response=webResource.entity(dsl).accept("application/json").get(ClientResponse.class);
String result = response.getEntity(String.class);
I use the dsl in Kibana and can get the correct query results. But it did not get the correct response in java and throw 406 error.
How can I fix this issue? Thanks very much!
You simply need to add another Content-type HTTP request header for the application/json mime type since you're sending some JSON and ES doesn't guess your content type anymore:
ClientResponse response = webResource
.entity(dsl)
.header("Content-type", "application/json") <--- add this
.accept("application/json")
.get(ClientResponse.class);

jersey 1.17 how to get reponse body when status returned in 401

I have a question on how to fetch response body in Jersey client when server returns some sample text with status code 401. Sample service is setup as follows:
#GET
#Path("test401withcontent")
public Response get401TestWithContent()
{
return Response.status(401).entity("return some text").build();
}
On the client side (using Jersey 1.17) ClientResponse.getEntity prints null.
Noticed that content-length of headers has the right number (16 in this case.)
Is there a different way to get response when return code is 401?
Have deployed you method to my test web site and used below client got currect response.
Client client = ClientBuilder.newClient();
//System.setProperty("sun.net.http.allowRestrictedHeaders", "true");
Response response = client.target(
"http://jerseyexample-ravikant.rhcloud.com/rest/jws/test401withcontent").
request().get(); System.out.println(response.readEntity(String.class));

400 Bad Request: POST request using javax.ws.rs and Json jackson

I am trying to make a POST with javax. Keep getting the 400 bad request response code. It suggests that there is something wrong with the json being sent(?) I have checked the json a hundred times, it looks pretty good to me. What am I missing?
enter code here
Client client = ClientBuilder.newClient();
Entity payload = Entity.json("{ 'offset': 0, 'limit': 15, 'query': 'ad', 'search_type': 'global'}");
Response response = client.target("https:******/contacts/search")
.request(MediaType.APPLICATION_JSON_TYPE)
.header("Accept", "application/json")
.header("authToken", ACCESS_TOKEN)
.post(payload);

Getting header values, Status code etc from Jax-Rs Response class in Junit

I am using Restful web Service using Dropwizard. And generating response as:
Response response = resources.client().resource("/url")
.header("CONTENT-TYPE","value")
.post(Response.class, jsonRequestString);
Now I want to write unit test to ensure the returned content type is corrected in Response Object. how to do that?
You can use the ClientResponse type in Jackson. For example, using a GET operation:
ClientResponse response = Client.create()
.resource(url)
.get(ClientResponse.class);
String contentType = response.getHeaders()
.getFirst("Content-Type");
System.out.println(contentType);

Categories

Resources