Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AFNetworking 2 Response Error (Content type: text/html and not JSON)

After trying nearly every response on the subject, I've come up without a working answer to my problem.

The problem: So I've implemented the uploading portion of my app using AFNetworking 2.0.3 after porting from AFNetworking 1.3:

-(void)commandWithParams:(NSMutableDictionary*)params onCompletion:(JSONResponseBlock)completionBlock {

    NSData* uploadFile = nil;
if ([params objectForKey:@"file"]) {
    uploadFile = (NSData*)[params objectForKey:@"file"];
    [params removeObjectForKey:@"file"];
}

 AFHTTPRequestOperationManager *manager = [[AFHTTPRequestOperationManager alloc] initWithBaseURL:[NSURL URLWithString:@"http://54.204.17.38"]];

 manager.responseSerializer = [AFJSONResponseSerializer serilizer];
 manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"application/json"];

 AFHTTPRequestOperation *apiRequest = [manager POST:@"/API" parameters:params constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {

     if (uploadFile) {
         [formData appendPartWithFileData:uploadFile name:@"file" fileName:@"photo.jpg" mimeType:@"image/jpeg"];
     }

} success:^(AFHTTPRequestOperation *operation, id responseObject) {
    completionBlock(responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    completionBlock([NSDictionary dictionaryWithObject:[error localizedDescription] forKey:@"error"]);
}];

[apiRequest start];

}

The error I get when using this code is "Request failed: unacceptable content-type: text/html" I know you might be wondering if the server is responding with proper JSON, and I have every reason to think it is after inspecting the response headers in my browser that say 'MIME type: application/json'. Also, I am using 'header('Content-type: application/json')' at the top of my API as well (PHP API). Now, if I change the serialization type to 'AFHTTPResponseSerializer' instead of 'AFJSONResponseSerializer', it will not spit out the JSON error, but it will give me a different error (a random unrecognized selector error).

Any thoughts on why I cannot seem to get a JSON response out of this method?

like image 414
Nochbag Avatar asked Dec 27 '13 04:12

Nochbag


2 Answers

You can set the AFHTTPSessionManager to accept any MIME Type:

manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/html"];
like image 112
guru Avatar answered Nov 02 '22 18:11

guru


Got it! So, turns out, unknowingly, although my API was returning valid JSON, matter examining the header response logged on the Xcode side of things (thru NSLog(@"Error: %@", error);), it was actually returning text/HTML because it wasn't actually hitting the correct file, it was getting re-routed by a header somewhere. After explicitly stating the API path to be /API/index.php and not just /API, it started returning the valid JSON! Next, after making sure the response was properly JSON serialized (using requestManager.responseSerializer = [AFJSONResponseSerializer serializer];), the app worked!

Hopefully this helps someone who was having the same issue :)

like image 41
Nochbag Avatar answered Nov 02 '22 19:11

Nochbag