AFNetworking calling clearAuthorizationHeader - ios

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];

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.

how to add custom header to AFNetworking upload task?

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)>"];

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

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.

How to cancel network request with afnetworking

Is there a way to cancel all network request (the request started by another method) before I do a network request with AFNetworking
I tried like below but not work:
- (void)sendRequest:(NSUInteger)page{
NSURL *aUrl = [NSURL URLWithString:#"http://www.abc.com/"];
AFHTTPClient *httpClientToCancel = [[AFHTTPClient alloc] initWithBaseURL:aUrl];
[httpClientToCancel cancelAllHTTPOperationsWithMethod:#"POST" path:#"product/like"];
[httpClientToCancel release];
... start a new request here .....
But not work. I just want to cancel all request (at least the request I wrote above) before I start a new request.
Thank you!
[[httpClient operationQueue] cancelAllOperations];
Don't Create new AFHTTPClient instance.
try "[self cancelAllHTTPOperationsWithMethod:#"POST" path:#"product/like"];
Both the other two answers are right. Don't Create new AFHTTPRequestOperationManager instance
#interface OperateCustomerView () <WYPopoverControllerDelegate>{
AFHTTPRequestOperationManager *manager;// = [AFHTTPRequestOperationManager manager];
}
- (void)viewDidLoad {
[super viewDidLoad];
manager = [AFHTTPRequestOperationManager manager];

Resources