One Star

tRest does not return a body when getting a 400 error

Hi,
I'm using talend to call some Rest webservices with the tRest component. I log the response body and http status code.
It works fine when I get 2XX http codes, but when my WS returns a 4XX status code, the tRest always returns a null value for the body.
However, the body contains a message explaining why an error occured, so I need to retrieve this body even if I get a 4XX error code.
Is this a bug in the tRest component ?
Thanks.
8 REPLIES
Community Manager

Re: tRest does not return a body when getting a 400 error

Hi
What's the result if you call the rest ws in browser? Do you get some message or null if it returns a 4xx status code? I think it is the same result in Talend or browser.
Shong
----------------------------------------------------------
Talend | Data Agility for Modern Business
One Star

Re: tRest does not return a body when getting a 400 error

Hi,
When I call the ws in browser, or with an http rest plugin, I get the expected error message.
I have inspected the tRest source code and I found that :
if(errorResponse_<%=cid%>!=null){
<%=conn.getName() %>.ERROR_CODE = errorResponse_<%=cid%>.getStatus();
}
else{
<%=conn.getName() %>.Body = restResponse_<%=cid%>;
}
It seems that the component never parses the body response when getting an http error.
I tried to modify the component but it didn't work, so I have coded my own rest component that uses the java API and it works perfectly.
One Star

Re: tRest does not return a body when getting a 400 error

Just discovered this and its quite a nasty issue anyone know if there is a fix or work around?
https://www.talendforge.org/forum/img/members/69801/mini_null_rest_error_body.png.png
Cheers Andy
Community Manager

Re: tRest does not return a body when getting a 400 error

Hi moinerus
No changes about the response on tRestClient component, catch the error code and error message by using the error link.
tRestClient--response--tLogRow_1
               --error---tLogRow_2
Best regards
Shong
----------------------------------------------------------
Talend | Data Agility for Modern Business
One Star

Re: tRest does not return a body when getting a 400 error

Hi Shong,
Thanks for the response the component in question is tRest not tRestClient and doesn't have and error row output...
Cheers M
One Star

Re: tRest does not return a body when getting a 400 error

Has anyone found a workaround for this?
The bug is in the tREST code:
                                row2 = new row2Struct();
if (errorResponse_tREST_1 != null) {
row2.ERROR_CODE = errorResponse_tREST_1.getStatus();
} else {
row2.Body = restResponse_tREST_1;
}

This code only sets the body of the response if there was no error message.
The body should always be set, even if there was an error.
One Star

Re: tRest does not return a body when getting a 400 error

Here is the current Talend Code for tREST:
String restResponse_tREST_1 = "";
try {
restResponse_tREST_1 = restResource_tREST_1
.post(String.class, context.json_request);
} catch (com.sun.jersey.api.client.UniformInterfaceException ue) {
errorResponse_tREST_1 = ue.getResponse();
}
// for output
row2 = new row2Struct();
if (errorResponse_tREST_1 != null) {
row2.ERROR_CODE = errorResponse_tREST_1.getStatus();
} else {
row2.Body = restResponse_tREST_1;
}

It needs to be changed to something like the following:
com.sun.jersey.api.client.ClientResponse restResponse_tREST_1;
try {
restResponse_tREST_1 = restResource_tREST_1
.post(com.sun.jersey.api.client.ClientResponse.class, context.json_request);
} catch (com.sun.jersey.api.client.UniformInterfaceException ue) {
errorResponse_tREST_1 = ue.getResponse();
}
// for output
row2 = new row2Struct();
row2.Body = restResponse.getEntity(String.class));
row2.ERROR_CODE = restResponse.getStatus();

Note that restResponse_tREST_1 was changed from a String to a Client Response.
Note that the first argument to .post was changed from String.class to ClientResponse.class
Note that row2.Body is now set to restResponse.getEntity(String.class)) and row2.ERROR_CODE is set to restResponse.getStatus()
With this code change, the response body becomes visible on errors.
One Star

Re: tRest does not return a body when getting a 400 error

I had the same problem. I found the solution to getting (at least) the http error message:
org.apache.http.impl.EnglishReasonPhraseCatalog.INSTANCE.getReason(row27.ERROR_CODE,null)

(where row27 is the row coming out of the tRest component)

Concatenating the ERROR_CODE with the result of the operation above, it looks like this: '401-Unauthorized'. So now it is a bit easier to understand in my tDie.
However, as others mentioned, I cannot get the more specific error information returned by the WS that I'm using, because the body comes back empty.
Note: you might need to download and add the HttpCore Apache library to your /talend_installation_path/configuration/lib/java. And you might also have to use a tLibrary load to load it in your job before calling it.