Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Post Request with AFHttpRequestOperationManager not working

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

NSDictionary *parameters = [[NSDictionary alloc] initWithObjectsAndKeys:@"john", @"name", @"[email protected]", @"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.

like image 746
Shabir jan Avatar asked Nov 18 '13 11:11

Shabir jan


1 Answers

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":   @"[email protected]",
                        @"password: @"xxxxx",
                        @"type":    @"1"}
               success:^(AFHTTPRequestOperation *operation, id responseObject) {
                   NSLog(@"JSON: %@", [responseObject description]);
               } 
               failure:^(AFHTTPRequestOperation *operation, NSError *error) {
                   NSLog(@"Error: %@", [error description]);
               }
];
like image 130
Ralfonso Avatar answered Oct 13 '22 06:10

Ralfonso