【发布时间】: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