Issue with response descriptor matching - restkit

I'm making a request to this url
https://myserver.com/api/users/53a8d8d7e4b06b32f108c4f3/settings
and sometimes this request succeeds, and sometimes it fails. Here is the code I'm using to add the mapping.
RKResponseDescriptor *responseGetDescriptor =
[RKResponseDescriptor responseDescriptorWithMapping:userSettingsInfoMapping
method:RKRequestMethodGET
pathPattern:#"/api/users/:uniqueId/settings"
keyPath:nil
statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];
When it fails, the HTTP status code is 200, the error code from RestKit is RKMappingErrorNotFound.
I get a helpful debug log from Restkit:
failed to match: response path '/api/users/53a8d8d7e4b06b32f108c4f3/settings' did not match the path pattern '/api/users/:uniqueId/settings'.
Just from looking at this log, the paths seem to match. I believe that is what generates the mapping error. Looking at keypaths of my json response, they are similar to the json returned in success scenario. The response is a 200.
Might anyone have ideas as to why this is happening?

Related

What status should be set used for returning validation errors on an entity in HttpStatus

What status should be set used for returning validation errors on an entity from org.springframework.http.HttpStatus
If we want to return validation errors if certain fields are empty, what status would be preferable to use
400 - Bad Request is the common response code sent if the request doesn't contain all the required parameters or if it is in a invalid format.
More on this: 400 BAD request HTTP error code meaning?

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));

RestKit: set expected status codes without using response descriptor

I need to PUT/POST data to a service. Upon success, the server returns 201/Created (no body). The parameters are (necessarily) a NSDictionary, so no object binding on either side is needed. RKObjectManager.requestWithObject works fine for this, but raises an error it expected a 204 rather than a 201. As far as I can tell, the only place to alter expected status codes is in a matching RKResponseDescriptor. RKResponseDescriptors seem like overkill for a response which has no body, and I'm unsure how to even construct one that works with no body. How can I tell RestKit that a 201 is OK for this POST?
Update
I eventually gave up on trying to do requests that didn't involve any sort of binding (ie, posting a dictionary and receiving a 201 response), and just dropped down to NSURLConnection stuff - it wasn't bad)
On your RKObjectRequestOperation:
operation.HTTPRequestOperation.acceptableStatusCodes = [NSIndexSet indexSetWithIndex:201];

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.

Resources