How to update the error code in IIB for my pass thro - ibm-integration-bus

I am getting 500 error code and its a SOAP format.
I see HTML, SOAP 1.1 & SOAP 1.2 formats available for response when there is an exception in my pass thro service for example when wrong endpoint request, it gives 500.
I want to change it to 400 or 404 for invalid request/not found.
Please advise for updating it for a SOAP/JSON response. It would be nice to know for both the format.

Its very simple:
Add a Compute Node, set the atribute "compute node" to "LocalEnvironment and Message", in the code you need:
For SOAP messages:
SET OutputLocalEnvironment.Destination.SOAP.Reply.Transport.HTTP.ReplyStatusCode = 404;
For REST messages:
SET OutputLocalEnvironment.Destination.HTTP.ReplyStatusCode = 500;

Related

Is there a way to get response on error with HTTPClient in Ruby?

I'm using the HTTPClient ruby library for making some basic http requests to a REST service I have develop and im wondering now how can I get the response from the service when an error occurred (a status code like 400 or 500 is returned from the service).
What my service does is return the stock of a product in a center, so it takes two parameters (in the url): a product id and a center id. When I call this service passing correct product and center ids the service returns me a 200 - OK status and the stock of the product (for example, 8.0 indicating there are 8 units of the product in that center). However, if I pass a wrong product id (one that is not in our database) the service returns 400 - Bad Request and an error message indicating that the id does not exists.
So I have a method like the following in Ruby
def stock(product, location)
response = #client.get_content("stock/#{product}/#{location}")
end
#client is an instance created with HTTPClient.new that I have configured. On passing ''right'' arguments it returns me what I want so it is well configured
My problem now is that when the service returns an error status code an HTTPClient::BadResponseError exception is thrown and I'm not able to retrieve this error in the response variable.
Is there a way to get the response from the service, no matter which status code it returns? I tried using begin...rescue and I can get some information like the status code and the reason phrase, but not the content of the response (the error message)
You're using get_content which skips a few steps and returns the content. Instead step back and use get which will return a more complete response structure that includes, among other things, the status field you want:
def stock(product, location)
response = #client.get("stock/#{product}/#{location}")
body = response.body
status = response.status
end

generic return type for restemplate.exchange

I am using resttemplate.exchange to invoke a URL and get response. But the issue is the response type varies when I successfully receives the output and if I get some error.
eg.
ResponseEntity<XXX[]> response = restTemplate.exchange(endPoint,HttpMethod.GET,req,
new ParameterizedTypeReference<XXX[]>() {},uriVariables);
If there is no issue with service and then output is in format of list. but if there is error like "NO DATA FOUND" then the response is in MAP. so whenever I have any issue with URL i get "404: null error" because my response type is unable to identify the error which is in MAP.
Could you please suggest what could be done as i can not change the response type of services.
Edit:: http://localhost:9090/data/getDetail?name=XXX
response [{"name":"XXX","Dept":"teaching","createdby":"YYY","createdDt":"06/09/2018"}]
when data not found case::http://localhost:9090/data/getDetail?name=YYY
response
{"response":"DATA NOT FOUND"}

JAX-RS GET: MessageBodyReader not found for media type=text/plain

I'm getting following error while trying to do JAX-RS GET request:
MessageBodyReader not found for media type=text/plain, type=class com.intuit.accountant.services.common.cdm.Job, genericType=class com.intuit.accountant.services.common.cdm.Job
Below is my code:
Response response = target("jobs/Hello")
.request()
.header("intuit_offeringid", "testOfferingId")
.header(RequestHeaders.REALM, CommonUtil.DEFAULT_REALM_ID_FOR_INTUIT_EMPLOYEE)
.header(RequestHeaders.AUTH, "002923")
.header(RequestHeaders.TICKET,"00303")
.get(Response.class);
What does this error mean? How can I fix this?
You need to post all the code. The error is almost assuredly not happening in that code sample you posted. The get(Response.class) is converting it to a generic http response where you can see the response payload, status, response headers etc.
What you didn't post would most likely look somemthing like this. response.readEntity(com.intuit.accountant.services.common.cdm.Job)
In this case you don't have a reader registered to convert a text/plain response from the server to an entity. I don't know if the response was supposed to be json/xml and you are receiving text because there was an error of some kind. You should check the response as text like this to see what you are getting. This will probably point you in the right direction. If you are getting text you would have to write an implementation of MessageBodyReader to convert the plain text into an entity.
Try this...
System.out.println("Response body is " + response.getEntity(String.class));

HttpWebRequest/Response throwing error "Not Found"

I am making an API call to a REST service. The REST service returns an XML string that contains a user token if the password submitted is correct, or an XML string with data if it isn't.
Here is an example if the password is incorrect:
<?xml version="1.0" encoding="utf-8" standalone="yes" ?>
<authenticationResponse>
<statusCode>403</statusCode>
<errors>
<error>
....
</error>
</errors>
<timestamp>2011-03-31 22:45:03 GMT</timestamp>
</authenticationResponse>
With this code below, it appears .NET is translating this to an actual error. I still want it to read the XML data and ignore any error:
RequestData requestData = (RequestData)result.AsyncState;
HttpWebResponse response =
(HttpWebResponse)requestData.Request.EndGetResponse(result);
How I can ignore the error but still create the stream to read the XML?
Catch WebException, check the exception status, read the response. See these questions for examples:
Catching a specific WebException (550)
WebException when reading a WebException's response stream
Your code isn't translating this into an actual error - a HTTP status code of 403 is "Forbidden".
HTTP "Not Found" has a HTTP status code of 404 so it looks like the HTTP endpoint you're requesting doesn't exist in the REST service.
Ok so I figured it out, there's a few parts here.
First off the API is generating that xml when the parameters passed don't meet whats expected. In this case, if the password is incorrect, it'll pass back the 403.
The error is an error, so the framework treats it as such, however the error contains a response anyways, you just have to get the response off the error. This is essentially the answer to the question. Have to catch it the error and snag the response off the error to read the data in the stream, which is what Mauricio was getting to.
Essentially, I guess all the answers here are correct, or pieces of it, just took a little digging to put it all together.
Thanks Guys.

Custom http codes

Could I use custom HTTP codes?
I want to use these codes as response for AJAX requests.
Example:
220 - will be correspond to status that some item was created successfully
420 - will be correspond to status that some validations errors were occurred
Each response will be has json string.
You can define extension codes, but it only makes sense if you want to standardize something; in which case you need to write a spec, and get the status code registered (see http://greenbytes.de/tech/webdav/draft-ietf-httpbis-p2-semantics-12.html#rfc.section.4.1).
If this is just between your server and your client, simply put the additional information into the response body and use a more generic status code.
That being said -- what you called "420" is already defined as "422 Unprocessable Entity".
Using your server side language of choice you can send headers to the browser with the relevant HTTP code and message.

Resources