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;
Best Answer
The problem is that the server is sending you an HTML encoded answer. You can do one of these 2 options:
Talk to the server side people, to encode it as a json.
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.