【问题标题】:How see the error of a json POST with AFNetworking?如何使用 AFNetworking 查看 json POST 的错误?
【发布时间】:2014-01-11 02:21:53
【问题描述】:

查看POST request with AFNetworking 2.0 - AFHTTPSessionManager后我看不到错误消息在哪里。

这是我的设置:

-(id)init
{
    if ((self = [super init])) {
        NSURL *url = [NSURL URLWithString:DebugURLString];

        self.session = [[AFHTTPSessionManager alloc] initWithBaseURL:url];

        self.session.requestSerializer = [AFJSONRequestSerializer serializer];
        self.session.responseSerializer = [AFJSONResponseSerializer serializerWithReadingOptions:NSJSONReadingAllowFragments];
    }

    return self;
}

-(void) login:(NSString *)email pwd:(NSString *)pwd result:(void(^)(NSError* result))handler
{
    NSDictionary *params = @{@"username": email, @"password":pwd};

    [self.session POST:@"auth/api-token/" parameters:params
               success:^(NSURLSessionDataTask *task, id responseObject) {
                   NSLog(@"%@", responseObject);
                   handler(nil);
               }
               failure:^(NSURLSessionDataTask *task, NSError *error) {
                   NSLog(@"%@", task);
                   NSLog(@"%@", error.userInfo);
                   handler(error);
               }];
}

我打电话给http://www.django-rest-framework.org/api-guide/authentication#tokenauthentication,直接是rest_framework.authtoken.views.obtain_auth_token,要求输入用户名和密码并返回一个令牌。

如果用户/密码正确,则此工作正常。但如果没有,我会得到:

error.userInfo[AFNetworkingOperationFailingURLResponseErrorKey]
<NSHTTPURLResponse: 0xb388f20> { URL: http://localhost:8000/auth/api-token/ } { status code: 400, headers {
    Allow = "POST, OPTIONS";
    "Content-Type" = "application/json";
    Date = "Sat, 11 Jan 2014 02:14:13 GMT";
    Server = "WSGIServer/0.1 Python/2.7.5";
    Vary = "Accept, Cookie";
} }

但是,当我使用 python + requests 进行相同的调用时,我得到:

{u'non_field_errors': [u'Unable to login with provided credentials.']}

但我在NSError 中找不到。

另外,尽管我设置了responseSerializer,它在任务中报告:

(lldb) po task.response
<NSHTTPURLResponse: 0xb388f20> { URL: http://localhost:8000/auth/api-token/ } { status code: 400, headers {
    Allow = "POST, OPTIONS";
    "Content-Type" = "application/json";
    Date = "Sat, 11 Jan 2014 02:14:13 GMT";
    Server = "WSGIServer/0.1 Python/2.7.5";
    Vary = "Accept, Cookie";

而不是 JSON 之一。我需要解决什么问题?

【问题讨论】:

    标签: json ios7 django-rest-framework afnetworking-2


    【解决方案1】:

    您遇到了AFNetworking v2 的怪癖 — 默认情况下,响应正文不会传递到失败块中。 Check the discussion on Github for the details.

    底线是您需要子类化AFJSONResponseSerializer,以便在responseObjectForResponse:data:error: 期间将所需的响应数据添加到NSErroruserInfo 字典中。 (然后您就可以访问它了。)

    您的问题促使我重新访问Github issue (same as above)。现在有a blog posta repo with an example subclass 可用。

    我希望这一切都会有所帮助。

    AFNetworking v2 很棒;这种行为在采用时是奇怪的。我最初的想法是“这不可能”,但我认为根本原因是鼓励您创建自己的响应序列化程序。随附的示例相当轻量级。创建您自己的很容易,并且响应序列化器是封装网络相关逻辑的好地方,否则最终会回到您的应用程序中。

    【讨论】:

    • 这成功了,但是正文返回一个 NSString 而不是 JSON/Dictionary...
    • 你会得到一个 NSData。如果你有一个字符串,你可以自己在某个地方转换它。如果你想要一个 NSDictionary,你可以在那里使用 NSJSONSerialization。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-19
    相关资源
    最近更新 更多