how to add custom header to AFNetworking upload task? - ios

i know how to add headers in normal to GET or POST request but i couldn't add custom header to upload manager
so if any can help me in that thanks

You can try this to add Header..
NSMutableURLRequest * request;
[request setValue:#"Add your value here" forHTTPHeaderField:#"Set field"];
For Example if you want to add cookie, then
[request setValue:#"frontend=322ybbnbgda6382392du" forHTTPHeaderField:#"Cookie"];

If you are using any of these:
AFURLRequestSerialization#multipartFormRequestWithMethod:URLString:parameters:
or
AFURLRequestSerialization#requestWithMultipartFormRequest:...
All of these methods returns a NSMutableURLRequest, to which you can add the headers using NSMutableURLRequest#setValue:forHTTPHeaderField:.
From there you can use a task, executing for example AFURLSessionManager#uploadTaskWithRequest:fromFile:progress:success:failure: or AFURLSessionManager#dataTaskWithRequest:success:failure: to send the request.

Using AFHTTPRequestOperationManager class you can add header.
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
[manager.requestSerializer setValue:#"<Your Header String>" forHTTPHeaderField:#"<Key(Parameer Name)>"];

Related

AFJSONResponseSerializer not allow underscore in the response body

I am using Afnetworking 3.1.0 for iOS app. My JSON response has a '_' (underscore) sing as bellow.
response --> {"_body":{"data":{"
Android app can serialize same response without any issue, which is using okhhtp3.
I suspect, AFJSONResponseSerializer is the culprit. I have tried as bellow to set acceptableContentType to AFJSONResponseSerializer. But does not work.
self.requestSerializer = [AFJSONRequestSerializer serializerWithWritingOptions:NSJSONWritingPrettyPrinted];
self.responseSerializer = [AFJSONResponseSerializer serializer];
self.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:#"application/json", #"text/json", #"text/javascript",#"text/html", nil];
Anyone has idea how to resolve this, please share.
Found the issue is not in AFNetworking. Problem is in the enumerateKeysAndObjectsUsingBlock method which I am using to construct a customize JSON from the response.
enumerateKeysAndObjectsUsingBlock is ignoring object inside an object.

AFNetworking calling clearAuthorizationHeader

I see this method in AFNetworking:
- (void)clearAuthorizationHeader {
[self.mutableHTTPRequestHeaders removeObjectForKey:#"Authorization"];
}
how would I call this method in another file? I tried the following:
#import "AFURLRequestSerialization.h"
AFHTTPRequestSerializer *clear;
and then calling it inside my logout method like so:
[clear.clearAuthorizationHeader];
but I get this error:
/Users/jsuske/Documents/SSiPad(Device Only)ios7/SchedulingiPadApplication/ViewControllers/LHLoginController.m:495:36: Expected identifier
To call a method, you need the space notation instead of a Dot. But you need also a valid object instance of the serializer, which you can get from the AFHTTPRequestOperationManager.
Here is an example code:
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
AFHTTPRequestSerializer <AFURLRequestSerialization> * requestSerializer = manager.requestSerializer;
[requestSerializer clearAuthorizationHeader];

AFNetworking 2.0: Passing header information

I am new to AFNetworking and I know how to pass URL parameters. But how would I pass headers into the same call.
I am also subclassing my AFHTTPSessionManager
See my code below:
- (void)getExpenses:(NSString *)page
success:(void (^) (NSArray *myExpenses))success
failure:(RequestFailureBlock)failure
{
NSString *resourceURL = [NSString stringWithFormat:#"%#/expenses/", APIBaseURLString];
NSDictionary *parameters = #{#"page":page, #"Authorization": APIAuthorization};
[self getExpenses:resourceURL parameters:parameters success:success failure:failure];
}
setAuthorizationHeaderFieldWithToken is deprecated due to servers having different requirements about how the access token is sent (token, bearer, etc)
michaels answer otherwise is correct, use
[self.requestSerializer setValue:#"Some-Value" forHTTPHeaderField:#"Header-Field"];
or
[self.requestSerializer setAuthorizationHeaderFieldWithUsername:#"" password:#""];
for basic auth
You set header values on the requestSerializer property of AFHTTPSessionManager:
[self.requestSerializer setValue:#"Some-Value" forHTTPHeaderField:#"Header-Field"];
EDIT:
It looks like you're trying to set authorization; there is a method for that too:
[self.requestSerializer setAuthorizationHeaderFieldWithUsername:#"" password:#""];
// OR
[self.requestSerializer setAuthorizationHeaderFieldWithToken:#""];
If you need to set the Content-Type header, see this SO answer on how to do that

RestKit post request parameter MIMEType

I'm working on an iOS app that was implement in RestKit .10 and am updating it to .20. One of the post requests the app makes has a block of data as a parameter. Previously, the app encoded this use RKParam setData:MIMEType:forParam; I'm looking for the .20 version of this for adding a parameter to a NSMutableDictionary parameter.
Old code:
NSString *logString; // loaded up elsewhere
NSData *textFileContentsData = [logString dataUsingEncoding:NSUTF8StringEncoding];
RKParams *params = [RKParams params];
[params setData: textFileContentsData MIMEType:#"text/plain" forParam:#"log_file"];
New code:
NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
[parameters setObject:???? forKey:#"log_file"]; // not sure how to get string here.
I have tried putting the textFileContentsData NSData object in for ???? but the data arrives in binary, which is not what is required. I need to figure out how to retain the text/plain MIMEType.
TIA
Janene
From your description I wouldn't use RestKit for this as there is no mapping going on, I'd use AFNetworking instead (vehicular is included in RestKit so you already have full access).
Using RestKit, you would use the object manager to create a request to send, something like:
NSMutableURLRequest *request =
[objectManager multipartFormRequestWithObject:nil
method:RKRequestMethodPOST
path:#"..."
parameters:nil
constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
[formData appendPartWithFormData:...
name:#"..."];
}];
Then you could use RKObjectRequestOperation to process the request. But I'm not convinced this is exactly what you need if you just want to set the mime type (just a header) and the post data, both of which can be done directly on NSMutableURLRequest.

After setting custom headers in AFNetworking 2.x, the parameters are not being received by the RESTful API

I followed the suggested solution at AFNetworking 2.0 add headers to GET request to specify custom headers for the request with the following code snippet:
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
manager.requestSerializer = [AFJSONRequestSerializer serializer];
[manager.requestSerializer setValue:someID forHTTPHeaderField:#"some_id"];
NSDictionary *parameters = #{#"id": user.id, #"birthday": user.birthday};
[manager POST:[NSString stringWithFormat:#"%#/user_create",BaseURLString] parameters:parameters
success:^(AFHTTPRequestOperation *operation, id responseObject)
{
if (responseObject[#"error"])
{
NSLog(#"REST User Create Response Error: %#", responseObject[#"error"]);
}
else
{
[self saveUserDetails:responseObject];
}
}
failure:^(AFHTTPRequestOperation *operation, NSError *error)
{
NSLog(#"REST User Create Error: %#", error);
}];
But what happens when this gets executed is I get an error in the response from the API stating all my parameters are missing. This same block of code used to work before (without setting the custom header and when the API didn't require them originally).
Does anybody know how to properly set both custom headers and POST parameters?
Thanks,
Nino
Your code looks fine to me.
A few things to try:
Try using AFNetworkActivityLogger to see what's actually being sent (set it to AFLoggerLevelDebug.) You can also use a web proxy like Charles or a protocol analyzer like Wireshark.
If you determine the data is not being sent properly, set a breakpoint in [AFURLRequestSerialization -requestBySerializingRequest:withParameters:error:]. This is where your HTTP headers and parameters are added to the URL request. The method is pretty straightforward; you should be able to step through and watch as stuff is added to the request and determine why it gets skipped.
NOTE: AFURLRequestSerialization.m contains multiple subclasses of AFURLRequestSerialization. Set a breakpoint in the super implementation, as well as in the AFJSONRequestSerializer implementation.
Examples that could cause this behavior:
parameters is nil.
you've added POST to HTTPMethodsEncodingParametersInURI but
your API is not prepared to handle parameters appended to a URL on a POST request, or
the queryStringSerialization block is nil AND queryStringSerializationStyle is set to something other than AFHTTPRequestQueryStringDefaultStyle.
NSJSONSerialization can't handle your parameters dictionary
One side note (unrelated to your problem), if you use AFHTTPRequestOperationManager's initWithBaseURL: method, and keep a strong reference to your manager, you won't have to do that [NSString -stringWithFormat:] stuff to construct your URL.
I discovered that I should have set my request serializer to the following instead since my API doesn't require JSON-formatted parameters.
manager.requestSerializer = [AFHTTPRequestSerializer serializer];
Hope this helps others dealing with the same situation as mine.

Resources