I have this code, I use cxf WebClient:
WebClient client = someClient.reset();
Response response = client.post(bodyRequest);
If status code in response turns into 200 I can parse it into something like this:
CustomResponse customResponse = response.readEntity(CustomResponse.class);
And that's ok, but if status code turns to be 400 or another, response entity seems to be null, so I can't find a way to parse it into an object ResponseCodeError, like this:
ResponseCodeError responseError= response.readEntity(ResponseCodeError.class);
This will fail.
Is there a way to use cxf and parse error into Custom error class?
Thanks.
You can check the status using
int code = response.getStatus();
then you check for a code of 200 to parse the entity or throw the respective error for other codes like 400.
Related
I have been struggling to simply perform a GET request to the Spotify API and parse the results of the request. I haven't been able to successfully do it despite trying to follow the docs. I am trying to achieve this within an Actor. I have tried making the HttpEntity a strict HttpEntity but this returns an error. I am not sure if this is due to dependency issues or if it is simply the incorrect approach. What I would like to do is obtain the entire payload of the response and parse it to then return it to another actor. I am unsure what should go inside of the try block to achieve this. Thanks very much.
ActorSystem actorSystem = getContext().getSystem();
Http http = Http.get(actorSystem);
String endpoint = "https://api.spotify.com/v1/artists/1moxjboGR7GNWYIMWsRjgG/top-tracks?market=IE";
Authorization authorization = Authorization.oauth2("REDACTED");
HttpRequest request = HttpRequest.create().withUri(endpoint).addHeader(authorization);
CompletionStage<HttpResponse> responseFuture = http.singleRequest(request)
responseFuture.thenAccept(response -> {
try {
}
Have tried casting entity to a strict entity but this results in an error.
I am trying to use littleproxy-mitm to inspect traffic. I have access to the headers and can easily read them. However, I cant find the body of a response consitently. To see if I can get the body I am using this testing my app by visiting https://www.google.com/humans.txt, but the wanted body is no where to be found. But when I visit other sites like google, facebook and twitter I seem to get gibberish(encoded body gzip most prob) and sometimes html.
Here is the filter:
#Override
public HttpObject serverToProxyResponse(HttpObject httpObject) {
if(httpObject instanceof FullHttpResponse){
System.out.println("FullHttpResponse ----------------------------------------");
FullHttpResponse response = (FullHttpResponse) httpObject;
CompositeByteBuf contentBuf = (CompositeByteBuf) response.content();
String contentStr = contentBuf.toString(CharsetUtil.UTF_8);
System.out.println(contentStr);
}
return httpObject;
}
Any idea why I am unable to get body from https://www.google.com/humans.txt ?
To answer my own question.
This code snippet works and will print the whole response. But the reason I was not getting the body response is either the header "Modified-since.." or the "Cache-control: public".
I am using Apache Camel HTTP component and I am able to send request and receive response.
In failure cases i get exception and if i try to get the HTTP Response code from headers, the response is null.
if(exchange.getException() != null ){
exchange.getException().printStackTrace();
String responseCode = (String) exchange.getOut().getHeader(Exchange.HTTP_RESPONSE_CODE);
}
exchange.getOut() is NULL and fails with NullPointerException.
How to retrieve the HTTPResponse Code in such cases? Ex: 400, 404, 405.
According to the documentation for the http-component you should be able to extract the response code from the Exception.
Perhaps something like this:
int code = ((HttpOperationFailedException)exchange.getException()).getStatusCode();
I have a code block like
final Invocation.Builder builder = webTarget.request();
final Entity<IFSRequestPresentation> entity = Entity.entity(ifsRequestPresentation, MediaType.APPLICATION_JSON);
final Response response = builder.post(entity);
The server (which is external to me and I can't see logs) is not returning me the data I expect. I believe that the json payload is not good
The IFSRequestPresentation is quite complex and I would like to see how it is represented as json String.
I am using Spring(4.0.3) and Jersey(2.8).
Is there a way I can log builder.post(entity) method? or at least see what entity looks like as json String?
I wrote it to file like
mapper.writeValue(new File("my.json"), IFSRequestPresentation);
as per Jackson documentation and got the json written to that file
I have the following problem...
I'm testing a service that return HTTP responses on GET requests.
My problem is that I would like to view the response even if it was an HTTP 500 / 404 or whatever response.
I would like to view that. But I can't because it throws an exception and that's it.
Is there a way to view a jersey response even if it was an error response?
My code is like this:
webResource = client.resource(url);
response = webResource.queryParams(alertParams)
.header("x-token", token).get(String.class);
So when get receives an error response from the service I wont be able to view that although the response is something like this:
{
"errCode" : "ERR002",
"errMsg" : "",
"techErrMsg" : "LoginFailureGeneric"
}
Which is a 400 Bad Request.
Thanks very much for all the help!!
This is where you need to spend some time with the docs... WebRequest#get(Class) will throw an exception when you get an HTTP error status if you are trying to parse the response as anything other than ClientResponse.
So all you need to do is change the .get(String.class) -> .get(ClientResponse.class) and you can pull the entity itself (and the status, and everything else) off of the ClientResponse object sans exceptions.