AFNetworking - 发布请求不起作用

时间:2014-04-30 11:25:54

标签: ios afnetworking

我正在使用AFNetworking。我的大多数请求都有效,但其中一个并不是出于某些原因。

  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];

  }];

我收到以下说明错误:

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绝对适用于相同的JSON和发布路径。我无法理解为什么错误地我得到另一个没有'v2'部分的基本网址以及为什么我得到错误500.

更新:

    + (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
                                };

3 个答案:

答案 0 :(得分:1)

我认为你应该设置contentType:“application / json”可能会有所帮助,请试试这个。

答案 1 :(得分:0)

你要回来的错误......

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

这意味着实际在服务器上运行的代码出现了问题

答案 2 :(得分:0)

最后,我发现花了一整天后问题出在哪里:

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

当我删除斜线标志时,一切都开始起作用了。