14

I am using AFHTTPRequestOperationManager to post some JSON to my server, my code is below.

NSDictionary *parameters = [[NSDictionary alloc] initWithObjectsAndKeys:@"john", @"name", @"xxxxx@gmail.com", @"email", @"xxxx", @"password", @"1", @"type", nil];
// Do any additional setup after loading the view.
AFSecurityPolicy *policy = [[AFSecurityPolicy alloc] init];
[policy setAllowInvalidCertificates:YES];
AFHTTPRequestOperationManager *operationManager = [AFHTTPRequestOperationManager manager];
[operationManager setSecurityPolicy:policy];

[operationManager POST:@"posturl here" parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) {
    NSLog(@"JSON: %@", [responseObject description]);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    NSLog(@"Error: %@", [error description]);
}];

and the response is as follows:

2013-11-18 16:49:29.780 SwapOnceApiTester[12651:60b] Error: Error Domain=AFNetworkingErrorDomain Code=-1011 "Request failed: unsupported media type (415), got 1664256" UserInfo=0x1565a6c0 {NSErrorFailingURLKey=xxxxxxx, AFNetworkingOperationFailingURLResponseErrorKey=<NSHTTPURLResponse: 0x15656db0> { URL: xxxxxxxxx } { status code: 415, headers {
    "Cache-Control" = "max-age=604800";
    Connection = "keep-alive";
    "Content-Type" = "application/json";
    Date = "Mon, 18 Nov 2013 11:49:28 GMT";
    Expires = "Mon, 25 Nov 2013 11:49:28 GMT";
    Server = nginx;
    "Transfer-Encoding" = Identity;
    "X-Powered-By" = PleskLin;
} }, NSLocalizedDescription=Request failed: unsupported media type (415), got 1664256}

I dont know what the problem is with this.

David Snabel-Caunt
  • 57,804
  • 13
  • 114
  • 132
Shabir jan
  • 2,295
  • 2
  • 23
  • 37

2 Answers2

29

You need to set your request and response serializers to handle JSON using AFJSONRequestSerializer and AFJSONResponseSerializer before executing your request. Using an NSDictionary literal for your parameters helps code clarity as well:

AFSecurityPolicy *policy = [[AFSecurityPolicy alloc] init];
[policy setAllowInvalidCertificates:YES];

AFHTTPRequestOperationManager *operationManager = [AFHTTPRequestOperationManager manager];
[operationManager setSecurityPolicy:policy];
operationManager.requestSerializer = [AFJSONRequestSerializer serializer];
operationManager.responseSerializer = [AFJSONResponseSerializer serializer];

[operationManager POST:@"posturl here" 
            parameters: @{@"name":  @"john",
                        @"email":   @"xxxxx@gmail.com",
                        @"password: @"xxxxx",
                        @"type":    @"1"}
               success:^(AFHTTPRequestOperation *operation, id responseObject) {
                   NSLog(@"JSON: %@", [responseObject description]);
               } 
               failure:^(AFHTTPRequestOperation *operation, NSError *error) {
                   NSLog(@"Error: %@", [error description]);
               }
];
Jirapong
  • 24,074
  • 10
  • 54
  • 72
Ralfonso
  • 1,614
  • 15
  • 30
  • hi thanks for replying, i have tried this as well but the result is same, this is the response i am getting now { status code: 415, headers { "Cache-Control" = "max-age=604800"; Connection = "keep-alive"; "Content-Length" = 0; "Content-Type" = "application/json"; Date = "Wed, 20 Nov 2013 06:52:24 GMT"; Expires = "Wed, 27 Nov 2013 06:52:24 GMT"; Server = nginx; "X-Powered-By" = PleskLin; } }, NSLocalizedDescription=Request failed: unsupported media type (415), got 0} , i dont know why the content-length is zero. – Shabir jan Nov 20 '13 at 06:55
  • Doesn't work for me. The parameter passed to server is empty. – Bagusflyer May 12 '14 at 15:30
0

Just solve my problem, my server was not configured to accept the charset utf8 with application/json so i just removed the charset utf for ajson serializer in Afnetworking 2.0 and now it is working correclty.

Shabir jan
  • 2,295
  • 2
  • 23
  • 37