I'm trying to make a GET request using AFNetowrking. Here's the code:
@property (nonatomic, readwrite) AFHTTPSessionManager *httpSessionManager;
...
NSURLSessionConfiguration *config = [NSURLSessionConfiguration defaultSessionConfiguration];
_httpSessionManager = [[AFHTTPSessionManager alloc] initWithBaseURL:[NSURL URLWithString:self.baseURL]
sessionConfiguration:config];
_httpSessionManager.responseSerializer = [AFJSONResponseSerializer serializerWithReadingOptions:NSJSONReadingAllowFragments];
_httpSessionManager.responseSerializer.acceptableContentTypes = [_httpSessionManager.responseSerializer.acceptableContentTypes setByAddingObject:@"text/html"];
_httpSessionManager.securityPolicy = [AFSecurityPolicy policyWithPinningMode:AFSSLPinningModeNone];
_httpSessionManager.securityPolicy.allowInvalidCertificates = YES;
...
NSMutableDictionary* parameters = [self defaultUserSessionParameters];
NSString *url = [self constructURLWithEndpointPath:@"lead_sources.json"];
RequestSuccessBlock winBlock = ^(NSURLSessionDataTask *task, id responseObject)
{
NSLog(@"GetLeadSources, response Object: %@", [responseObject description]);
NSArray* leadSources = responseObject[@"lead_sources"];
if (!leadSources)
{
NSString* errorString = @"Successfully retrieved lead sources but no values were returned!";
NSLog(@"%@",errorString);
NSError* error = [NSError bpdErrorWithDescription:errorString];
completion(nil, error);
}
else
{
completion(leadSources,nil);
}
};
RequestFailureBlock failureBlock = ^(NSURLSessionDataTask *task, NSError *error)
{
NSLog(@"FAILURE: get lead sources, Error: %@", error);
NSString* ErrorResponse = [[NSString alloc] initWithData:(NSData *)error.userInfo[AFNetworkingOperationFailingURLResponseDataErrorKey] encoding:NSUTF8StringEncoding];
NSLog(@"server error response: %@ /end", ErrorResponse);
};
[self.httpSessionManager GET:url
parameters:parameters
progress:nil
success:winBlock
failure:failureBlock];
But I get this error every time:
Request failed: unacceptable content-type: text/html
I did some digging and found out that this error is thrown because of my response serializer so I added this line to fix the issue:
_httpSessionManager.responseSerializer.acceptableContentTypes = [_httpSessionManager.responseSerializer.acceptableContentTypes setByAddingObject:@"text/html"];
But when I add this I get the following error:
JSON text did not start with array or object and option to allow fragments not set.
For this I found out that I need to set the reading options to allow fragments. I tried setting the serializer to this:
_httpSessionManager.responseSerializer = [AFJSONResponseSerializer serializerWithReadingOptions:NSJSONReadingAllowFragments];
but then I get another error:
Invalid value around character 0
I think I have to set the response serializer to be an AFHTTPResponseSerializer but I can't seem to figure out how to allow fragments with that serializer.
Either way, I don't think this is why the 404 is being thrown. I believe I'm not hitting the domain I'm trying to hit and AFNetworking is having issues parsing the response thrown by the url I'm hitting.
So two questions:
How do I make it so that my responses are properly parsed by the serializer? (aka silence these three errors?)
Why is the 404 being thrown? I can navigate to the url that it's trying to hit but it says page not available
Thanks in advance!
From the comments:
So it turns out the default is the json serializer. But overall, I think the way I was thinking about it was incorrect. The responses returned from the server I setup are all JSON, so i should be using the default. My problem was that I wasn't hitting the endpoint, so the response being returned was HTML, so it was throwing two errors, one that we failed to hit the endpoint (404) and another that it can't parse text/html. The reason the second was being thrown was because I wasn't hitting the endpoint so fixing that issue fixed the other.