Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AFNetworking Expected content type error

I am getting the json string in failure block

 NSURL *url = [[NSURL alloc] initWithString:@"http://www.vinipost.com/Services/Update/UpdateService.asmx/GetPropSubType?"];
        NSURLRequest *request = [[NSURLRequest alloc] initWithURL:url];
        [AFJSONRequestOperation addAcceptableContentTypes:[NSSet setWithObject:@"text/html"]];

        AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) {

            NSLog(@"%@", JSON);
        } failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON) {
            NSLog(@"Request Failed with Error: %@, %@", error, error.userInfo);
        }];
        [operation start];

Output:

Request Failed with Error: Error Domain=AFNetworkingErrorDomain Code=-1016 "Expected content type {(
    "text/json",
    "text/javascript",
    "application/json",
    "text/html"
)}, got text/plain" UserInfo=0x71521a0 {NSLocalizedRecoverySuggestion=[{"PropTypId":1,"PropCatId":1,"PropTyp":"Flat/ Condo"}.......**
like image 390
Vivek Sehrawat Avatar asked Jul 15 '13 06:07

Vivek Sehrawat


3 Answers

You need to add this line before operation

[AFJSONRequestOperation addAcceptableContentTypes:
                            [NSSet setWithObject:@"text/plain"]];
like image 50
Stan Avatar answered Nov 12 '22 09:11

Stan


The error is clear: the web service is returning a wrong content type. The content type should be one of these:

"text/json", "text/javascript", "application/json", "text/html"

But it returns

text/plain

Moreover, if you look at the http response, it returns HTML TAGS inside it, so AFNetworking is not able to parse.

If this page:

http://www.vinipost.com/Services/Update/UpdateService.asmx/GetPropSubType?

is under your control, correct the behavior removing html tags and changing the content type

like image 37
LombaX Avatar answered Nov 12 '22 10:11

LombaX


In AFNetworking, you have to create NSURLRequest with the help of AFHTTPClient(So first you have to create AFHTTPClient and have to set some properties for this object) like below

AFHTTPClient *httpClient = [[httpClient alloc] initWithBaseURL:[NSURL URLWithString:@"http://www.vinipost.com/"]];

[httpClient registerHTTPOperationClass:[AFJSONRequestOperation class]];
    [httpClient setDefaultHeader:@"Accept" value:@"application/json"];
    httpClient.parameterEncoding = AFJSONParameterEncoding;

now if depends of GET/POST or any other type request you need to set parameter I consider it as POST Request, so set the Parameter dict and set all required Key Value pairs properly.if no parameters required you can pass Parameters as nil

NSURLRequest *request = [httpClient requestWithMethod:@"POST" path:@"Services/Update/UpdateService.asmx/GetPropSubType?" parameters:params];
AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request
                                     success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON)
                                     {
                                         NSLog(@"%@",JSON);

                                     }
                                     failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON)
                                     {
                                         NSLog(@"Error MSG = %@",error);
                                     }];

[operation start];

hope this will work for you :)

like image 4
Suryakant Sharma Avatar answered Nov 12 '22 09:11

Suryakant Sharma