Search code examples
objective-cjsonafnetworking-2

AFNetworking JSON post using Objective C returns unsupported media type


I am trying to access a Microsoft CRM Web Service and I am using AFNetworking to do that. The service is configured to handle JSON request and response.

But when I do a JSON post I am getting this error:

"com.alamofire.serialization.response.error.data=<>, NSLocalizedDescription=Request failed: unsupported media type (415)} "

What is the Mistake i have done on the code.

This is my code:

   AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];

    manager.requestSerializer = [AFJSONRequestSerializer serializer];
    manager.responseSerializer = [AFJSONResponseSerializer serializer];

    [manager.requestSerializer setValue:@"application/json" forHTTPHeaderField:@"Accept" ];
    [manager.requestSerializer setValue:@"application/json; charset=utf-8" forHTTPHeaderField:@"Content-Type" ];

    NSDictionary *parameters = @{@"username": self.Username.text,
                                 @"password": self.Password.text};

    [manager POST:@"http://www.xxxxxx.com/xxxx.svc/xxxxx" parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSLog(@"JSON: %@", responseObject);
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"Error: %@", error);
    }];

Solution

  • Googling for "status 415" finds this straight from the horses mouth:

    "The server is refusing to service the request because the entity of the request is in a format not supported by the requested resource for the requested method."

    In other words, the server doesn't support application/json.