Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AFNetworkingErrorDomain Code=-1011 "Request failed: internal server error (500)"

I am using Afnetwork 2. I am a new one for IOS development. I want to connect some API using POST method.

In this API I want set the Header as well.

This is my code

AFHTTPSessionManager *manager = [[AFHTTPSessionManager alloc] initWithBaseURL:baseURL];

    manager.responseSerializer.acceptableContentTypes =[manager.responseSerializer.acceptableContentTypes setByAddingObject:@"text/html"];
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/html"];
    [manager.requestSerializer setValue:token forHTTPHeaderField:@"X-Auth-Token"];
    manager.responseSerializer = [AFJSONResponseSerializer serializer];

    [manager POST:@"api/v1/contact-group/add/" parameters:params

    success:^(NSURLSessionDataTask *task, id responseObject) {


    } failure:^(NSURLSessionDataTask *task, NSError *error) {


    }];

When I use this code I am getting the error like this

Error Domain=AFNetworkingErrorDomain Code=-1011 "Request failed: internal server error (500)" UserInfo=0xa8b0930 {NSErrorFailingURLKey=http://xxxxxx.info/api/v1/contact-group/add, NSLocalizedDescription=Request failed: internal server error (500), NSUnderlyingError=0xa88cb00 "Request failed: unacceptable content-type: text/html", AFNetworkingOperationFailingURLResponseErrorKey=<NSHTTPURLResponse: 0xaa5a940> { URL: http://xxxxxx.info/api/v1/contact-group/add } { status code: 500, headers {
    "Cache-Control" = "no-cache";
    Connection = close;
    "Content-Type" = "text/html";
    Date = "Thu, 12 Jun 2014 15:39:31 GMT";
    Server = Apache;
    "Set-Cookie" = "laravel_session=.....";
    "X-Frame-Options" = SAMEORIGIN;
    "X-Powered-By" = "PHP/5.3.28, PleskLin";
} }}

I refer lot of documents and stackoverflow questions and answer. They gave lot of answers. But Unfortunately I didn't get the solution. Please help me.

Please point my mistake.

NOTE: When I use without header it will return "Invalid header". It will return correct error message.

EDIT

Server expecting format Method POST

input
{
    "blog_guid": "k39453050345j",
    "contact_groups": [
        "er234we232",
        "tet343i49ti3409t33"
    ]
}

server response

{
    "error": false,
    "code": 200,
    "message": "Success"
}

I am sending parameters like this

 NSMutableDictionary *params = [[NSMutableDictionary alloc]
                                   initWithObjects:@[guid, groupName, description]
                                   forKeys:@[APISEND_CONTACTGROUPKEY, APISEND_CONTACTGROUPNAME, APISEND_CONTACTGROUPDESCRIPTION]];
like image 362
Puvanarajan Avatar asked Feb 03 '26 23:02

Puvanarajan


1 Answers

According to the elements you've added in your edited version, it seems like you're not sending out JSON data in the format that is expected by the server.

The server is expecting a dictionary with blog_guid and contact_groups items, the latter itself being an array of strings. You have to carefully comply with these requirements in your own code.

In your code, you're sending a dictionary with 3 items: guid, groupName, and description. Also, there are no arrays for the group names.

Try with something like this:

NSArray *groupNames = [[NSArray alloc] initWithObjects:groupName];
NSMutableDictionary *params = [[NSMutableDictionary alloc]
                               initWithObjects:@[guid, groupNames]
                               forKeys:@[APISEND_BLOGGUID, APISEND_CONTACTGROUPKEY]];

Also, check that APISEND_CONTACTGROUPKEY equals to @"contact_groups" and APISEND_BLOGGUID equals to @"blog_guid".

To help you debugging in case it still doesn't work, try to print out to the console the value of the params variable once it is serialized to JSON by AFNetworking, and check that it is similar to the format you've described in your question (the one that works).

like image 154
Romain Avatar answered Feb 05 '26 14:02

Romain



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!