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.
Related
I have followed the cook books guide to the letter, found here https://echo.labstack.com/cookbook/jwt
But when using the JWT middleware I am having some issues with adding custom error messages. Login works fine, even to the point of not giving details (username & password) that returns a 404.
But when the JWT is missing it returns a 400, I want it to also return a 404.
So in my research I found this, https://forum.labstack.com/t/custom-error-message-in-jwt-middleware/325/3 which lists the following middleware.ErrJWTMissing & middleware.ErrJWTInvalid But is very unclear on how to set these?
I have tried setting them as vars on the router file, like so
var (
ErrJWTInvalid = echo.NewHTTPError(http.StatusTeapot, "test 104")
ErrJWTMissing = echo.NewHTTPError(http.StatusTeapot, "test 103")
)
But the error that sill comes back to me is a 400 and not a 418 (as this is just a test). So what am I doing wrong?
You can change the HTTP code and message this way.
func init() {
middleware.ErrJWTMissing.Code = 401
middleware.ErrJWTMissing.Message = "Unauthorized"
}
First, a point on your statement that you want to return a 400 and also a 404 error - you cannot do this. You're sending one response from the server so it gets exactly one response code. You could send a 207, but we're not really talking about multiple resources here, so don't do that. In my opinion, a 400 error is indeed the correct response for a missing JWT as that constitutes a bad request. A 404 "Not Found" means that the requested resource (the thing on the server side) could not be found. It does not mean that something in the request could not be found.
As for setting your custom error message, you're likely to be out of luck without altering the source code for Echo. That specific response is coming from within the middleware handlers of the package itself (you can see it here). This is mostly abstracted away from you, so without looking at the inner workings of the package, there would be no way to tell where this was coming from, and frankly there's not a lot that you can easily do about it. ErrJWTMissing is indeed the variable that the package uses internally for this error message, but Echo does not appear to provide an exported setter method for you to change this value, so you're stuck with what it is.
If you truly wanted to set a custom error method for this case I think your options would be to:
Write your own middleware to intercept the request before it was handled by Echo's middleware, where you could handle the request however you wanted.
Edit the Echo source to work how you wanted it to work -- specifically, all you would have to do is edit ErrJWTMissing.
Basically, Echo is trying to do you favors by handling all of this middleware processing for you, and it's a lot of work or hackery to un-do that work while still using Echo.
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;
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"}
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));
In iOS I am using AFNetworking for calling the web-services. Here I am getting the error: "unacceptable content type: application/excel". After a lot of struggle I came to know that it is the server team problem. I have a question regarding this:
Before returning the response of service call to the app, notification is being sent and at the same time the notification information(text, time, etc) is logged in notification.xls file on the server where the header is set for the file like below,
function calledWhenServiceCalled(){
someJsonArray; //generate some json array
saveNotification();
return someJsonArray;
}
function saveNotification(){
//some functionality
header('Content-Type: application/excel');
//save the file to .csv
}
If I comment out the 'header('Content-Type: application/excel');' line above then the service call does not return any error and returns proper json response. But if the line is retained then I get unacceptable content type error(application/excel) from the server.
Please let me know why is the content type set for the file header is being sent as unaaceptable content-header for service response? There is no connection between the file created and service response right?? I am not getting what the problem is.. Please help... Let me know If I have not made myself clear...