I would like to modify HTTP headers of the request responses using AFHTTPSessionManager (the final goal is to modify the Cache-Control header in order to force caching).
I didn't find any delegate, block in order to do such things. Did I miss something? Maybe by subclassing AFHTTPSessionManager? Any idea how to do this the right way?
Regards,
Quentin
I hope this is what you need
[sessionManager.requestSerializer setValue: maxAge forKey:#"cache-control"];
And you can use this for referring how to set-up the header.
You can similarly set-up sessionManager.responseSerializer
Related
I want to add additional fields to NSMutableURLRequest (for exapmle NSString value requestID) in order to determine correct handler for request when NSURLSession completes it.
Is it legal to create a custom NSMutableURLRequest's subclass to add specific fields? Apple documentation has no additional information about NSMutableURLRequest subclassing.
UPDATE:
I discovered that NSMutableURLRequest subclassing is not the best idea: background NSURLSession can't create download task using my custom subclass object: method downloadTaskWithRequest: always returns nil. I think this problem related with mutableCopyWithZone: that called by NSURLSession when it creates download task with request's copy.
Thanks.
I had no problem creating the task with my subclass, but when I tried to access my custom field from task.originalRequest I discovered it is a NSMutableURLRequest, not my custom subclass.
Instead of extending NSMutableRequest, I would Suggest create basic N/w call handler which would accept your custom parameter.
In this class itself you can use NSMutableRequest to create findal Request with given paramters.
This class can be used application wide to serve you response / data for any request.
I think that if Apple does not provide any warning of subclassing a class, then you can do it.
It seems to me that the proper place to do this is in AFURLSessionManager, in setting the taskWillPerformHTTPRedirection block, but I am unsure of the best way to handle it.
Currently, in my AFHTTPSessionManager subclass, I am setting the redirect block globally for all requests, and I know I can prevent redirects by returning nil here:
- (void)setupRedirectBlock {
[self setTaskWillPerformHTTPRedirectionBlock:^NSURLRequest *(NSURLSession *session, NSURLSessionTask *task, NSURLResponse *response, NSURLRequest *request) {
return nil;
}];
}
...but I need to only do this on specific tasks, and there doesn't appear to be a way to get this information from the task itself.
I guess I am looking for some sort of user info dictionary or something I can use to set a flag telling this method to either return the request or return nil. Currently, it looks like I would have to do a string comparison on the response/request URL in the client where it is far away from where the task and path is actually created.
So this begs the question, am I fighting convention, or is there really no better way to intercept an AFNetworking 2.0 redirect on a task-by-task basis?
setTaskWillPerformHTTPRedirectionBlock is the best way to intercept redirects. The session manager is responsible for determining when or when not to prevent redirects based on the request associated with the task. In most cases, the path of the request should be a sufficient determination, but the user could additionally tag information in a custom request header field.
I have the same question, but unfortunately I don't yet have a good enough answer. One workaround could be using taskDescription https://developer.apple.com/library/ios/documentation/Foundation/Reference/NSURLSessionTask_class/Reference/Reference.html#//apple_ref/occ/instp/NSURLSessionTask/taskDescription
Just put there some constant like DO_NOT_FOLLOW_REDIRECT and check for it in your setTaskWillPerformHTTPRedirectionBlock block.
I'm new to AFNetworking but so far liking the abstraction.
I'm creating a subclass of AFHttpClient and I'd like to set the parameter encoding to JSON but ONLY for POST requests, is this possible?
You can do this by overriding requestWithMethod:path:parameters:, and setting the parameter encoding according to the specified method. Since all requests created by the client go through this method, it will work as expected.
I have to communicate with a REST API with a custom authorization scheme. It uses a authorization header witch I need to set based on the content of the request, so the server can check that I known the scheme.
I would like to use RestKit and its powerful Core Data utilization but I found it difficult to find a neat way to set this header for every different request. There isn't a thing like a delegate on RKObjectManager that is called before every request.
Maybe I missed something, could someone tell me if there is an easy way to do this? Thanks in advance.
You can do something like
[RKObjectManager sharedManager] postObject:yourObjectToPost usingBlock:^(RKObjectLoader *loader) {
NSDictionary* httpHeaders =#{#"key1":#"value1",
#"key2":#"value2",
#"key3":#"value3"};
loader.additionalHTTPHeaders = httpHeaders;
loader.delegate = self;
}];
While using a 3rd party API, I have the requirement to cancel all traffic when a custom response header is set to a certain value. I am trying to find a nice place to do this check only once in my code (and not in every success/failure block, where it works fine). From what I understand, this could be done by overriding -(void)enqueueHTTPRequestOperation:(AFHTTPRequestOperation *)operation in my custom AFHTTPClient subclass, but when I implement it like that:
-(void)enqueueHTTPRequestOperation:(AFHTTPRequestOperation *)operation
{
NSLog(#"[REQUEST URL]\n%#\n", [operation.request.URL description]);
NSLog(#"[RESPONSE HEADERS]\n%#\n", [[operation.response allHeaderFields] descriptionInStringsFileFormat]);
[super enqueueHTTPRequestOperation:operation];
}
the response headers are nil. Can anybody help me with that?
At the moment when operations are being created and enqueued in AFHTTPClient, they will not have the response from the server--that will be assigned when the request operation is actually executed.
Although the requirement to cancel all traffic seems unorthodox (at least if outside of the conventions of HTTP), this is easy to accomplish:
In your AFHTTPClient subclass, add a BOOL property that stores if requests should be prevented, and then used in enqueueHTTPRequestOperation. Then, override HTTPRequestOperationWithRequest:success:failure: to execute the specified success block along with some logic to set the aforementioned property if the salient response is present.