Denis Kutlubaev
Denis Kutlubaev

Reputation: 16114

AFNetworking - Post request doesn't work

I'm using AFNetworking. Most of my requests work, but one doesn't for some reason.

  RRHTTPClient *client = [RRHTTPClient sharedClient];
  NSLog(@"Base url: %@", client.baseURL);
  // Here I get base url: http://api-ios.rabota.ru/v2/

  [client postPath:responsesPath parameters:responseDic success:^(AFHTTPRequestOperation *operation, id responseObject) {

    [MBProgressHUD hideHUDForView:self.view animated:YES];

  } failure:^(AFHTTPRequestOperation *operation, NSError *error) {

    NSLog(@"Error: %@", [error description]);

    [MBProgressHUD hideHUDForView:self.view animated:YES];

  }];

I get error with the following description:

Printing description of error:
Error Domain = AFNetworkingErrorDomain Code = -1011 "Expected status code in (200-299), got 500"
UserInfo = 0x15ed43e0 {
    NSLocalizedRecoverySuggestion = {
        "error": {
            "code": 500,
            "message": "Class 'ExpressResponseREST' not found"
        }
    }, AFNetworkingOperationFailingURLRequestErrorKey = < NSMutableURLRequest: 0x15d6c960 > {
        URL: http: //api-ios.rabota.ru/express-response/ }, NSErrorFailingURLKey=http://api-ios.rabota.ru/express-response/, NSLocalizedDescription=Expected status code in (200-299), got 500, AFNetworkingOperationFailingURLResponseErrorKey=<NSHTTPURLResponse: 0x15ee2d50> { URL: http://api-ios.rabota.ru/express-response/ } { status code: 500, headers {
        "Cache-Control" = "no-cache, must-revalidate";
        Connection = "keep-alive";
        "Content-Language" = ru;
        "Content-Type" = "application/json; charset=utf-8";
        Date = "Wed, 30 Apr 2014 11:12:22 GMT";
        Expires = 0;
        "Keep-Alive" = "timeout=3";
        Pragma = "no-cache";
        Server = "nginx/1.4.2";
        "Transfer-Encoding" = Identity;
        Vary = Accept;
        hst = "backend-mobile";
    }
  }
}

API definitely works with the same JSON and post paths. I can't understand why in error I get another base url without 'v2' part and why I get error 500.

Update:

    + (RRHTTPClient *)sharedClient
    {
      static dispatch_once_t onceToken;

      dispatch_once(&onceToken, ^{

        sharedClient = [[self alloc] initWithBaseURL:[NSURL URLWithString:[RRHelper apiDomain]]];
        [sharedClient setParameterEncoding:AFJSONParameterEncoding];
        [sharedClient setDefaultHeader:@"Content-type" value:@"application/json"];

      });

      [sharedClient setDefaultHeader:@"X-Authorization" value:[[RRSession sharedSession] getSessionId]];

      return sharedClient;
    }

Update2 :

  NSString *responsesPath   = [NSString stringWithFormat:@"/express-response/"];

  NSDictionary *responseDic = @{
                                @"returnOfferId": returnOfferId,
                                @"name": name,
                                @"surname": surname,
                                @"mail": mail,
                                @"birthYear": @(birthYear),
                                @"birthDay": @(birthDay),
                                @"birthMonth": @(birthMonth),
                                @"offerExperienceYearCount": @(offerExperienceYearCount),
                                @"phone": phone
                                };

Upvotes: 0

Views: 522

Answers (3)

Denis Kutlubaev
Denis Kutlubaev

Reputation: 16114

Finally, I found where the problem was after spending whole day:

NSString *responsesPath   = [NSString stringWithFormat:@"/express-response/"];

When I removed slash signs, everything started to work.

Upvotes: 0

Flexicoder
Flexicoder

Reputation: 8501

The error you are getting back...

    "error": {
        "code": 500,
        "message": "Class 'ExpressResponseREST' not found"

This means there is something wrong with the code that is actually running on the server

Upvotes: 0

M.Alatrash
M.Alatrash

Reputation: 1265

I think you should set contentType: "application/json" might be help please try this.

Upvotes: 1

Related Questions