RestKit view all headers for RKObjectRequestOperation - ios

How can I get all the headers that are set for the RKObjectRequestOperation. I need to view the headers as there is something strange happening after a user has logged out, and want to view all headers before the operation starts.
Also is restkit caching authorization headers? I've tried setting the value in the manual request to an empty string and used [[RKObjectManager sharedManager].HTTPClient clearAuthorizationHeader]; to try clearing them, but the server is still receiving the Authorization header.

You can print defaultHeaders to check what is actually set in headers.
[[[RKObjectManager sharedManager] HTTPClient] defaultHeaders]

Related

iOS: NSURLCache with NSURLSession alongside HTTP headers

NSURLCache is some kind of a dark art magic that is neither documented properly, nor behaves as expected.
I am using AFNetworking's AFHTTPSessionManager to utilise NSURLSession. I am using this mocking service to create custom HTTP responses.
I am setting the NSURLCache with disk capacity and all the cache policies.
Then in the URLSession:task:didCompleteWithError method of NSURLSessionTaskDelegate, I am doing this:
NSURLRequest *request = task.currentRequest;
NSCachedURLResponse *cachedResponse = [[NSURLCache sharedURLCache] cachedResponseForRequest:request];
The documentation states about the cachedResponseForRequest:
#result The NSCachedURLResponse stored in the cache with the given
request, or nil if there is no NSCachedURLResponse stored with the
given request.
AFNetworking's FAQ states:
So long as your NSURLRequest objects have the correct cache policy,
and your server response contains a valid Cache-Control header,
responses will be automatically cached for subsequent requests.
After all this my assumption is that the value of the the cachedResponse variable will be nil if the Cache-Control is NOT set properly. However, whatever I have tried to do so far - setting it to 'private', 'no-cache' or whatever - the cachedResponse ALWAYS contains the cached response. How can I verify that the caching mechanism works as expected ? Is there anything that I am missing from the setup ?
I have been doing my tests by firstly making the request online, then switching off the internet on my computer. XCode 7.0 beta iOS 9.0.
Any help is appreciated.

AFNetworking content type for HEAD request for ios8

I have a HEAD request to certain image with If-modified-since HTTP header, in order to check if image is actually modified. If modified, then i just send GET request for this image.
It used to work fine for iOS 7, And it works now for iOS 7. But for iOS 8 i got
Error Domain=AFNetworkingErrorDomain Code=-1016 "Request failed: unacceptable content-type: image/png"
So the same code works for ios7 and for ios8- don't. Any ideas?
First, you don't need to make a HEAD request. An appropriate NSURLRequestCachePolicy can accomplish the same thing.
Second, the error is caused by an unacceptable content type being sent by the response for the response serializer. For image/png, use AFImageResponseSerializer, or set the appropriate acceptableContentTypes property for the response serializer of your choice.
Thanks Matt for showing the way. I removed HEAD request. Now i use AFHTTPRequestOperation with AFImageResponseSerializer. Also i set 'If-Modified-Since' header in NSMutableURLRequest with setValue:forHTTPHeaderField method. This date is stored in userdefaults, where key is URL, and value is the date where this URL was modified.
I'm sure there should be much better way for this, like using NSURLCache, but i couldn't do it.

How to get response headers of a request URL from UIWebView Delegates?

I need response headers of the request URL that I need to recieve and process in UIWebview Delegate method webViewDidFinishLoad: ? But I don't understand how to achieve it. Please note that I don't need request headers but response headers.
One idea is to create a NSUrlConnection and fire the request again to recieve it in completion block but I don't want to make two calls. There must be some way that it is handled by UIWebView and response headers can be retrieved . I just don't know how to go about it. Any ideas are welcome.
Try this:
NSCachedURLResponse *resp = [[NSURLCache sharedURLCache] cachedResponseForRequest:webView.request];
But this only works for cacheable urls!

Object mapper for xml in RestKit

I am trying map error response from server xml. In version 0.21 of restkit it worked ok. In last version it did not (0.22 and above). Possible response from server is <authorization-fail/>. Many server functions can lead to this response.
+ (void)addAuthErrorMapping:(RKObjectManager*)objectManager
{
RKObjectMapping* errorMapping = [RKObjectMapping mappingForClass:[RKErrorMessage class]];
[errorMapping addPropertyMapping:[RKAttributeMapping attributeMappingFromKeyPath:nil
toKeyPath:#"errorMessage"]];
RKResponseDescriptor* errorResponseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:errorMapping
method:RKRequestMethodGET
pathPattern:nil
keyPath:#"authorization-fail"
statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];
[objectManager addResponseDescriptor:errorResponseDescriptor];
}
In 0.21 version I am getting dictionary with authorisation-fail key. In last versions result is empty dictionary.
This should never have worked.
The server should be returning the response to you with a non-2xx HTTP response code, and you should be using that code to trigger your response descriptor.
As it is, you're triggering the response descriptor all the time and telling it to look for - and drill into - the authorisation-fail. So, if that key exists but is empty you should get an RKErrorMessage that's empty.
If you have some other response descriptor that matches the response before this one (i.e. any other with a nil path pattern and success response code) then this one will never work and that is why you're getting an empty dictionary.
So, the best option is to change the server response, and the workaround solution is to use a dynamic mapping which checks the content and provides either a good response or an error mapping to be used.

Retrieving HTTPBody from RestKit response

I am using the postObject and putObject functions of restKit. I need to extract some info from the payload and put it in the header before sending it out.. any suggestions on how I can do that?
ex:
[[RKObjectManager sharedManager] postObject:object path:OBJECT parameters:nil success:success failure:failure];
I will probably have to do something like
// Create Payload for this request
// update header accordingly
// call postObject
I would like to avoid throwing away existing code..
I was able to do this by using all the params that I was passing before making the RK request.

Resources