To check POST requests, I use a small POSTMAN utility. My settings: enter image description here

I use AFNetworking to send a POST request. Here is my code:

AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager]; NSDictionary *parameters = @{@"offset": @"1", @"csrf-token": @"63b164ea557b4aba694f81520c71972fa43a9772"}; manager.requestSerializer = [AFJSONRequestSerializer serializer]; [manager POST:@"http://simple.com/" parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) { NSLog(@"JSON: %@", responseObject); } failure:^(AFHTTPRequestOperation *operation, NSError *error) { NSLog(@"Error: %@", error); }]; 

But I get this error:

 Error: Error Domain=com.alamofire.error.serialization.response Code=-1011 "Request failed: bad request (400)" UserInfo=0x7a6c5240 {com.alamofire.serialization.response.error.response=<NSHTTPURLResponse: 0x7a6b5360> { URL: http://simple.com/ } { status code: 400, headers { "Cache-Control" = "no-store, no-cache, must-revalidate, post-check=0, pre-check=0, private, must-revalidate"; Connection = "keep-alive"; "Content-Encoding" = gzip; "Content-Type" = "text/html; charset=utf-8"; Date = "Sun, 31 May 2015 15:40:26 GMT"; Expires = "Thu, 19 Nov 1981 08:52:00 GMT"; Pragma = "no-cache"; Server = "nginx/1.6.2"; "Set-Cookie" = "csrf-token=5589461e87b28a538953ccac4168bf1f98b20b1ds%3A40%3A%22f52bda5262260a3ee0a10efcf48a6a49c11e4103%22%3B; path=/, PHPSESSID=803db60816b9f8203fb8feca01c130b6; path=/"; "Transfer-Encoding" = Identity; Vary = "Accept-Encoding,User-Agent"; "X-Powered-By" = "PHP/5.5.17"; 

I also tried to use NSURLConection. Code:

 NSString *post = @"offset=1&csrf-token=63b164ea557b4aba694f81520c71972fa43a9772"; NSData *postData = [post dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:YES]; NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]]; NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init]; [request setURL:[NSURL URLWithString:@"http://simple.com/"]]; [request setHTTPMethod:@"POST"]; [request setValue:postLength forHTTPHeaderField:@"Content-Length"]; [request setValue:@"application/x-www-form-urlencoded; charset=UTF-8" forHTTPHeaderField:@"Content-Type"]; [request setHTTPBody:postData]; NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self]; NSHTTPURLResponse *response = nil; NSError *error = nil; NSData *respData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error]; NSLog(@"~~~~~ Status code: %d", [response statusCode]); 

But I get error 400 again

Who can help solve the problem?

    3 answers 3

    AFNetworking most likely gives an error because of the response from the server, which comes in the form of text / html:

     "Content-Type" = "text/html; charset=utf-8"; 

    According to the idea of ​​Content-Type should be application / json.

    Or change the requestSerializer.

    • On the server goes Content-Type: application / x-www-form-urlencoded; charset = UTF-8. From Content-Type server: text / html; charset = utf-8 - runia

    I solved my problem.

    The fact is that the server stores a session with a specific CSRF token . My mistake was that I used the same token, both in the browser and in the mobile device. Therefore, the server did not give the data, because it received an incorrect POST request.

    My solution: I first get the token from the server CSRF , and then for further POST requests I use the received token. In response to a POST request, I receive the status 200 and the data I need.

    True, AFNetworking did not quite help me in this issue, so I had to use the standard NSURLConnection (I applied the code in the question). May someone come in handy for the future!

    Thanks to all! Good luck to all!

      Your request to AFNetworking is serialized as JSON, and in Postman - as form-data. Therefore, AFNetworking does not work.