Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AFNetworking Request failed: unacceptable content-type: text/html

I'm trying to send POST request to server, and I'm still failing to do so. Please help me out what's incorrect.

Shared instance init:

- (id)init{
self = [self initWithBaseURL:[NSURL URLWithString:kBaseURL]];
if(self) {
    self.
    self.responseSerializer = [AFJSONResponseSerializer serializer];
    [self.requestSerializer setAuthorizationHeaderFieldWithUsername:@"user" password:@"password"];
    parsingQueue = dispatch_queue_create("com.company.queue.parser",NULL);
}
return self;

post method body:

NSMutableDictionary *params = [NSMutableDictionary dictionaryWithObjectsAndKeys:[FSApplicationManager sharedInstance].userToken, @"user_token", nil];
[params setObject:[NSNumber numberWithDouble:userId] forKey:@"user_id"];
[params setObject:[NSNumber numberWithDouble:filmId] forKey:@"film_id"];
[params setObject:[NSNumber numberWithBool:NO] forKey:@"sendCopy"];

if([subject length]>0){
    [params setObject:subject forKey:@"subject"];
}
[params setObject:message forKey:@"message"];

NSString *URLString = [NSString stringWithFormat:@"%@%@", kBaseURL, kAPISendMessageToUser];

NSMutableURLRequest *request = [self.requestSerializer requestWithMethod:@"POST"
                                                               URLString:URLString
                                                              parameters:params
                                                                   error:nil];

[request addValue:[self signatureWithURL:[request.URL absoluteString] requestBody:[[NSString alloc] initWithData:[request HTTPBody] encoding:NSUTF8StringEncoding]] forHTTPHeaderField:@"X-SERVAPI-Signature"];
[request addValue:pub forHTTPHeaderField:@"X-SERVAPI-PublicKey"];

AFHTTPRequestOperation *operation = [self HTTPRequestOperationWithRequest:request success:^(AFHTTPRequestOperation *operation, id response){
    NSLog(@"%@", response);

} failure:^(AFHTTPRequestOperation *operation,  NSError *error){
    NSLog(@"%@, \n response %@", error, operation.responseObject);
}];

[self.operationQueue addOperation:operation];

return operation;

error I'm getting:

Error Domain=AFNetworkingErrorDomain Code=-1011 "Request failed: internal server error (500)" UserInfo=0xcf0c2f0 {NSErrorFailingURLKey=https://ppd.someserver.com/api/user/send-message, NSLocalizedDescription=Request failed: internal server error (500), NSUnderlyingError=0xcf06ce0 "Request failed: unacceptable content-type: text/html", AFNetworkingOperationFailingURLResponseErrorKey= { URL: https://ppd.someserver.com/api/user/send-message } { status code: 500, headers { Connection = close; "Content-Encoding" = gzip; "Content-Length" = 1412; "Content-Type" = "text/html; charset=UTF-8"; Date = "Sun, 14 Dec 2014 13:46:57 GMT"; P3P = "CP=\"NOI ADM DEV PSAi COM NAV OUR OTRo STP IND DEM\""; Server = Apache; Vary = "Accept-Encoding"; "X-Powered-By" = "PHP/5.3.3-7+squeeze19"; } }},

I've tried to contact guy responsible for server and that is what I get:

There is no need to add the content type to your request but if you wish to do so, since your are using a POST method, you should use: Content-Type: application/x-www-form-urlencoded;

like image 923
ninja_iOS Avatar asked Dec 14 '14 13:12

ninja_iOS


2 Answers

The problem is that the server is sending you an HTML encoded answer. You can do one of these 2 options:

  1. Talk to the server side people, to encode it as a json.

  2. Attempt to fix it by yourself by doing the following: After setting your self.responseSerializer put this line:

    self.operationManager.responseSerializer.acceptableContentTypes = [self.operationManager.responseSerializer.acceptableContentTypes setByAddingObject:@"text/html"];

This line adds "text/html" to the acceptable types. Providing that server's response is still serializable - you should be ok.

like image 187
o.shnn Avatar answered Oct 09 '22 04:10

o.shnn


You are receiving HTML response. To diagnose what's going on, it's useful to see what that HTML actually says.

Temporarily change the response serializer to AFHTTPResponseSerializer and then examine the HTML response and it might provide some information that will enlighten you regarding what's wrong with the request. Or you could leave the code alone and observe the exchange with Charles or other similar tool. But the goal is to see why you got a status code of 500 and the HTML a might give you some clues.

Regarding why the request isn't working, it's hard for us to say without seeing the API. The mix of key name styles in the request is suspicious (underscores in some, camelCase in others), but we don't know the API, so it's not clear if that is a problem or not.

Regarding the Content-Type, AFNetworking will do that for you (and will use application/x-www-form-urlencoded, so I don't think that's the issue.

If the API support team isn't familiar with Objective-C/AFNetworking code samples, you might want to observe the request using a tool such as Charles or something like that. Then capture the raw request getting sent and perhaps the API support team can better diagnose the problem without the distraction of the Objective-C/AFNetworking code.

like image 36
Rob Avatar answered Oct 09 '22 04:10

Rob