When I use AFHTTPRequestOperationManager, I can use the HTTPRequestOperationWithRequest method with NSURLRequest object. With this object, I can configure the request object with http body in which I can put a xml file.
Now I try to use the newer AFHTTPSessionManager, I only can use the GET, POST, etc. How I can put a xml file in the http request's body? Thanks.
In AFNetworking 2, a new object called the "request serializer" is how you are supposed to create your request body. There is no built-in serializer for posting XML. You'll need to subclass AFHTTPRequestSerializer, and set it as your manager's request serializer, like so:
[AFHTTPSessionManager manager].requestSerializer = [YourXMLRequestSerializer serializer];
When you subclass AFHTTPRequestSerializer, you'll need to override requestWithMethod:URLString:parameters:error: to return an NSMutableURLRequest with your desired content.
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.
What's the best practice to Logout and clean up the Authorization Header, etc. with RESTKit 0.20?
Will this method suffice?
RKObjectManager *objectManager = [self getObjectManager];
[objectManager.HTTPClient clearAuthorizationHeader];
If you use one of the setAuthorizationHeaderField* methods on the http client to add the authorisation then calling clearAuthorizationHeader is the correct approach to take.
If you explicitly set the header or parameter then you need to clear that header or stored attribute.
My example on iOS 6:
10 Multi-Part requests need to be sent (in order) to the server.
(so the request forms a queue)
progress should be shown.
if one request fails all following should fail
a request queue should be cancellable
Can AFNetworking help me with this? Or should I try to build something with NSOperations and run the loops myself?
If I need to pass context data between theses requests for example a transaction id produced by the first request. Are there any considerations about thread visibility I need to consider?
AFNetworking can do this. I recommend that you use AFHTTPRequestOperationManager (which itself uses NSOperation), rather than AFHTTPSessionManager. There are ways to do it with AFHTTPSessionManager, but none as elegant as with operations.
Under the hood, here's what you'd do without the manager:
You will use a request serializer to make your NSMutableURLRequest (for example, [AFHTTPRequestSerializer -multipartFormRequestWithMethod:URLString:parameters:constructingBodyWithBlock:error:]; there's a similar JSON request serializer too).
Once you have a URL Request, make the operation with [AFHTTPRequestOperation -initWithRequest:]. You should also set its completion blocks.
Finally, add your operation to [AFHTTPRequestOperationManager manager].operationQueue and start it.
Now that you understand how this is basically all working together, here's a simpler approach:
Subclass AFHTTPRequestOperationManager, optionally setting the requestSerializer if you don't like the default
Override (or copy with new implementation) -POST:parameters:constructingBodyWithBlock:success:failure:] - what you want to do is NOT start your operation right away.
Set the NSOperation dependency chains
start the first one
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;
}];