AFJSONResponseSerializer not allow underscore in the response body - ios

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.

Related

IOS - Basic Authentication with a REST API - SSL - AFNetworking 3.0

Everything works great with the code, the problem is it says authentication failed, though the username and password is 100% correct, so not sure if there is a way to pass the login and the password and get the user authenticated
NSString *urlString = #"URL";
NSMutableArray * keyStrings = [NSMutableArray new];
NSMutableArray * valueStrings = [NSMutableArray new];
[keyStrings addObject:#"user"];
[valueStrings addObject:#"abc"];
[keyStrings addObject:#"password"];
[valueStrings addObject:#"12345"];
NSDictionary * requestDict = [[NSDictionary alloc] initWithObjects:valueStrings forKeys:keyStrings];
AFHTTPSessionManager* manager = [AFHTTPSessionManager manager];
manager.responseSerializer = [AFHTTPResponseSerializer serializer];
manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:#"application/xml"];
manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:#"text/html"];
manager.securityPolicy.allowInvalidCertificates = YES;
manager.securityPolicy.validatesDomainName = NO;
I'm guessing you're intending to use HTTP Basic Authentication in which case you should be using the Authorization header field instead of passing the username and password in plain text within the request's parameters (I made this mistake the first time I tried to do Basic Authentication with a REST API).
You will need to add it to the AFHTTPRequestSerializer's headers which you can do by utilizing the setAuthorizationHeaderFieldWithUsername:password: method or by constructing the header field value manually and setting the header field value using the setValue:forHTTPHeaderField: method.

AFNetworking an empty array as a parameter

I'm trying to pass an empty array as a parameter.
[self POST:#"offers.json" parameters:#{ #"offer": #{ #"key": [NSArray new] } } success:success failure:failure];
But AFNetworking cuts that parameter and didn't sent it.
Any help?
As I understand, it's common issue and not AFNetworking bug, this line of code helped me
sessionManager.requestSerializer = [AFJSONRequestSerializer serializer];
https://github.com/AFNetworking/AFNetworking/issues/1722

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

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