5th consecutive RestKit call not being sent - ios

I'm sending an objectManager postObject call through RestKit. After 4 attempts that return 403's my 5th attempt isn't picked up and sent out by RestKit. I've verified this using Charles to watch my actual traffic, and by reviewing the RestKit logs, RestKit is getting my request and putting it in the RequestQueue, but it's never taken back out.
My first 4 requests look like this in the RestKit logs (apologies for the wall of log):
D restkit:RKObjectManager.m:171 CacheTimeoutSet: 60.000000 - 60.000000
T restkit.network.queue:RKRequestQueue.m:226 Request <RKObjectLoader: 0x784ac90> added to queue <RKRequestQueue: 0x6501530>
T restkit.network.queue:RKRequestQueue.m:173 Processing request <RKObjectLoader: 0x784ac90> in queue <RKRequestQueue: 0x6501530>
T restkit.network.queue:RKRequestQueue.m:104 Loading count increasing from 0 to 1. Firing requestQueueDidBeginLoading
T restkit.network.queue:RKRequestQueue.m:131 Loading count set to 1 for queue <RKRequestQueue: 0x6501530>
T restkit.network.cache:RKRequestCache.m:106 Found cachePath '<REMOVED>' for <RKObjectLoader: 0x784ac90>
T restkit.network.cache:RKRequestCache.m:123 Determined hasResponseForRequest: <RKObjectLoader: 0x784ac90> => NO
D restkit.network:RKRequest.m:255 Sending asynchronous POST request to URL <REMOVED>.
D restkit.network:RKObjectLoader.m:295 POST or PUT request for source object <TSLAuthenticationContract: 0x7849b30>
Username:<REMOVED>
Campus:<REMOVED>, serializing to MIME Type application/json for transport...
D restkit.object_mapping:RKObjectMappingOperation.m:391 Starting mapping operation...
T restkit.object_mapping:RKObjectMappingOperation.m:231 Mapping attribute value keyPath 'username' to 'Username'
T restkit.object_mapping:RKObjectMappingOperation.m:241 Mapped attribute value from keyPath 'username' to 'Username'. Value: <REMOVED>
T restkit.object_mapping:RKObjectMappingOperation.m:231 Mapping attribute value keyPath 'password' to 'Password'
T restkit.object_mapping:RKObjectMappingOperation.m:241 Mapped attribute value from keyPath 'password' to 'Password'. Value: <REMOVED>
T restkit.object_mapping:RKObjectMappingOperation.m:231 Mapping attribute value keyPath 'campusName' to 'CampusName'
T restkit.object_mapping:RKObjectMappingOperation.m:241 Mapped attribute value from keyPath 'campusName' to 'CampusName'. Value: <REMOVED>
D restkit.object_mapping:RKObjectMappingOperation.m:397 Finished mapping operation successfully...
T restkit.network.cache:RKRequestCache.m:106 Found cachePath '<REMOVED>' for <RKObjectLoader: 0x784ac90>
D restkit.network.cache:RKRequestCache.m:220 Read nil cached headers from cachePath '<REMOVED>' for '<RKObjectLoader: 0x784ac90>'
D restkit.network.cache:RKRequestCache.m:245 Found cached ETag '(null)' for '<RKObjectLoader: 0x784ac90>'
T restkit.network:RKRequest.m:209 Prepared POST URLRequest '<NSMutableURLRequest <REMOVED>>'. HTTP Headers: <REMOVED>.
D restkit.network.queue:RKRequestQueue.m:180 Sent request <RKObjectLoader: 0x784ac90> from queue <RKRequestQueue: 0x6501530>. Loading count = 1 of 4
T restkit.network.queue:RKRequestQueue.m:142 Timer initialized with delay 0.300000 for queue <RKRequestQueue: 0x6501530>
<REMOVED - Multiple Timer messages>
T restkit.network.queue:RKRequestQueue.m:142 Timer initialized with delay 0.300000 for queue <RKRequestQueue: 0x6501530>
D restkit.network:RKResponse.m:105 NSHTTPURLResponse Status Code: 403
However, on my 5th call this happens:
D restkit:RKObjectManager.m:171 CacheTimeoutSet: 60.000000 - 60.000000
T restkit.network.queue:RKRequestQueue.m:226 Request <RKObjectLoader: 0x782a370> added to queue <RKRequestQueue: 0x6501530>
T restkit.network.queue:RKRequestQueue.m:142 Timer initialized with delay 0.300000 for queue <RKRequestQueue: 0x6501530>
<REMOVED - Multiple Timer messages>
T restkit.network.queue:RKRequestQueue.m:142 Timer initialized with delay 0.300000 for queue <RKRequestQueue: 0x6501530>
As you can see, no Processing request log, no Loading count log, nothing after adding the request to the queue. Has anyone experienced this before? Is there some internal counter that is preventing RestKit from making this call again? I have users that aren't very technical and have issues with there passwords and so it's not inconceivable that they could fail to authenticate more than 4 times in a row.
EDIT:
After more research it appears that RestKit isn't decrementing the loadingCount flag in the objectLoader when it gets a failed response. Unfortunately I'm not sure how I should go about fixing this. Is it as simple as checking for the 403 in my delegate and removing the request manually if I find it?
EDIT 2:
Ok, I was able to force the removal of the failed request by calling RKRequestQueue cancelRequest: in my objectLoader:didFailWithError: delegate method. Not sure if this is the correct way, but it works. I would expect that RestKit internally handled this issue.
Thanks,
Rob

happend to me also
I noticed i got bad exec , so i run the Xcode Profiling and i noticed i was releasing the object running the request to early
after retaining it properly it was solved

I put my solution in edits to my question, but to make SO happy here it is again in an accepted answer.
After more research it appears that RestKit isn't decrementing the
loadingCount flag in the objectLoader when it gets a failed response.
Unfortunately I'm not sure how I should go about fixing this. Is it as
simple as checking for the 403 in my delegate and removing the request
manually if I find it?
Ok, I was able to force the removal of the failed request by
calling RKRequestQueue cancelRequest: in my
objectLoader:didFailWithError: delegate method. Not sure if this is
the correct way, but it works. I would expect that RestKit internally
handled this issue.

Related

AWS Lambda Serverless endpoint exits without executing function

We have a POST endpoint in our serverless api which listens to a Magento 2 integration activation callback and processes the payload. The Content-Type of this callback request is application/x-www-form-urlencoded. However, when we try to get the callback, the lambda function finishes execution immediately, skipping the entire function body. What we see in the Cloudwatch logs is only this. Not even console.logs are printed. (the endpoint only prints a string to the console. No async operations are in place. Yet this problem persists)
2020-12-12T12:24:47.012+05:30 START RequestId: 4afba03d-54ef-4b5e-bd44-157b0b7a9f9b Version: $LATEST
2020-12-12T12:24:47.050+05:30 END RequestId: 4afba03d-54ef-4b5e-bd44-157b0b7a9f9b
2020-12-12T12:24:47.050+05:30 REPORT RequestId: 4afba03d-54ef-4b5e-bd44-157b0b7a9f9b Duration: 37.83 ms Billed Duration: 38 ms Memory Size: 128 MB Max Memory Used: 109 MB Init Duration: 893.79 ms
When we try to hit the same endpoint from POSTMAN with Content-Type: application/json, the endpoint works as expected.
Therefore we thought that the problem might be the Content-Type header and read somewhere that adding request mapping templated would solve this problem. Therefore, we even added a mapping template for content type application/x-www-form-urlencoded in the integration request of the lambda method with following content, time to time. But our problem was not solved unfortunately.
"{ "body": "$util.base64Decode($input.body)" }"
{
"formparams" : $input.json('$')
}
{
"body" : $input.json('$')
}
My question is: How we can set the endpoint to print the POST request payload, preventing it from immediate exiting?
We have been searching for a solution to this problem since a week. It would be a great help, if someone can input their helpful, valuable suggestions to solve this problem. Thanks in advance
Since the Content-Type of the Magento 2 Integration activation callback is application/x-www-form-urlencoded, the lambda event for that POST request was something like this.
console.log(event) -> {body: "a=var&b=other_var&c=another_var"}
The endpoint didn't even print anything because I had put console.log(JSON.parse(event. body)). This results in a JSON parse error and the endpoint immediately finishes execution.
When I started parsing the query parameter event body instead of JSON.parse(), the problem was solved.

iOS Alamofire - Streaming JSON lines first response issue

Using Alamofire 4.9.0.
I am trying to implement handling streaming APIs in JSON lines format. Here's how:
stream = Alamofire.request(url, method: HTTPMethod.get,
headers: TTSessionManager.headers)
.validate()
.stream(closure: { (data) in
// parsing JSON lines ...
})
.response(completionHandler: { (response) in
// error handling ...
})
Now the issue is that the first response takes some time to return. And when it does I get a couple of JSON lines in one big batch. After that stream continues to normally respond with a new JSON line per response coming through the stream.
Has anyone encountered this behaviour? I'm wondering wether there is some additional session or request setup needed in order for this to work normal (line per response) from the start. When inspecting the response.metrics after canceling the request a lot of the fields are null so I can't for sure say wether some of the initial connection steps are the issue:
(Domain Lookup Start) (null)
(Domain Lookup End) (null)
(Connect Start) (null)
(Secure Connection Start) (null)
(Secure Connection End) (null)
(Connect End) (null)
So the problem here was that the response header didn't have Content-Type set to application/json. When this header is not set properly, URLSession data task will buffer first 512 bytes of response.
More info can be found here: https://developer.apple.com/forums/thread/64875

Parse custom value from NSError

When API request fails, I need to know number of failed attempts. Backend has added a property ‘attempts’ to error object. How can I find this value, since NSError is not KV pairing compliant and I don’t see it when I log the error object to console.
If you are using NSURLSession for urlrequest , then you will have delegate methods for NSURLSession responses , they are:
->didReceiveResponse = here you receive the response that urlrequest has completed and response received status
->didReceiveData = here you get the SUCCESS response date for your request
->didCompleteWithError = here you get the FAILURE response (i.e) ERROR callback ,this is called every time when request fails , which gives you error data

How to send a XML formatted POST request with RestKit 0.20.1

I have an iOS app that is using RestKit 0.20.1 to pull data from a server. The server at this time can send JSON formatted data but is not able to receive JSON formatted data.
This is where my problem is. POST requests require HTTP Body and the server is set up to receive XML. I have implemented the RKXMLReaderSerialization add on so I can receive XML but I can't find any current way of sending an XML formatted HTTP Body with RestKit.
This question "Send post request in XML format using RestKit " is what I was looking for but the answer from Imran Raheem is now (as far as I can tell) obsolete due to changes in RestKit.
I am using this method for the POST
[[RKObjectManager sharedManager] postObject:nil path:#"/rest/search?ip=255.255.255.0" parameters:search success:nil failure:nil];
Here is what the RestKit objectManager says about the postObject method
/**
Creates an `RKObjectRequestOperation` with a `POST` request for the given object, and enqueues it to the manager's operation queue.
#param object The object with which to construct the object request operation. If `nil`, then the path must be provided.
#param path The path to be appended to the HTTP client's base URL and used as the request URL. If nil, the request URL will be obtained by consulting the router for a route registered for the given object's class and the `RKRequestMethodPOST` method.
#param parameters The parameters to be reverse merged with the parameterization of the given object and set as the request body.
#param success A block object to be executed when the object request operation finishes successfully. This block has no return value and takes two arguments: the created object request operation and the `RKMappingResult` object created by object mapping the response data of request.
#param failure A block object to be executed when the request operation finishes unsuccessfully, or that finishes successfully, but encountered an error while parsing the resonse data. This block has no return value and takes two arguments:, the created request operation and the `NSError` object describing the network or parsing error that occurred.
If I have the MIMEType set to JSON the Trace shows my request.body is being populated like so request.body={"Search":"Trending"}.
However if I set the MIMEType to XML the Trace shows the request.body=(null)
Here is the line I use to change the MIMEType
[RKObjectManager sharedManager].requestSerializationMIMEType = RKMIMETypeJSON;
I am pretty new to iOS and Objective-C so I may be setting up the NSDictionary that is used in the parameters of the `postObject' method wrong. Here it is just in case....
NSArray *objects =[NSArray arrayWithObjects:#"trending", nil];
NSArray *keys =[NSArray arrayWithObjects: #"Search",nil];
NSDictionary *params = [NSDictionary dictionaryWithObjects:objects forKeys:keys];
self.search=params;
Any help would be greatly appreciated! Given that I am new Snippets are especially helpful!
Oh and BTW if anyone can point me to REST method that accepts JSON input I would gladly pass it on to my server guy so I can just avoid XML all together.
As feared and suggested in other posts on this topic writing XML is not supported in version of RestKit 0.20.1. Here is a link to my conversation with Blake Watters on the subject.
https://github.com/RestKit/RestKit/issues/1430#issuecomment-19150316
I never did clear up why the request.body= (null) when the MIMEType is set to XML.
The manager of the server has agreed to set it up to receive JSON. That is how I plan to work around this.

With iOS asynchronous http request, how do you connect the received data to the request?

I'm loading certain images from a certain server asynchronously. I'm firing a number of requests at once using NSURLConnection connectionWithRequest method and receive the data using NSURLConnectionDelegate didReceiveData.
At didReceiveData, how do I know which request this data matches? At didReceiveResponse I can use the URL method of the response given as a parameter, but in didReceiveData I only have the received data.
It seemed like the perfect answer would be to use NSURLConnection sendAsynchronousRequest, as the completion handler has all the required parameters: (NSURLResponse*, NSData*, NSError*). I can use [response URL] to make a match to the original request... except in one case: not all the images I try to download exist. In that case, the request is redirected to a generic error page and the URL of that generic page is received, so I can't match the response to a request I've made. This I could handle with connectionWithRequest, but I can't get the best of both worlds.
In
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
you can use
NSURLRequest *request = [connection originalRequest];
to get the request that the connection was started with.
(This method is available since iOS 5.0, but I could not find it in my Xcode iOS 5.1 Library. You find it in the iOS 6 Library, in the header file NSURLConnection.h or here: http://developer.apple.com/library/ios/#documentation/Cocoa/Reference/Foundation/Classes/NSURLConnection_Class/Reference/Reference.html#//apple_ref/occ/instm/NSURLConnection/originalRequest).
More methods to manage multiple connections can be found in this thread: Managing multiple asynchronous NSURLConnection connections.
If you use sendAsynchronousRequest:queue:completionHandler: then you can just use the request parameter in the completion block.
At connection:didReceiveData: the first parameter is the NSURLConnection instance. So I don't understand where the problem is. You create a connection, then you send a request to that connection and the delegate receive the connection:didReceiveData with the connection value.
If you are using the same delegate for all the request you have to check the connection so you can say which request is associated to.
Perhaps you have to maintain a table of connection/request pairs.

Resources