AFNetworking空请求正文

时间:2013-10-16 20:42:37

标签: ios objective-c afnetworking afnetworking-2

我正在尝试使用新的AFNetworking 2.0版本发出一个简单的请求,我似乎无法发布它。我从服务器“Expecting text / json或application / json body”得到了回复,但是根据AFNetworking的GitHub页面上的文档,我正在尽我所能。还值得一提的是,似乎我的代码最后一行中的operation.request.HTTPBody似乎总是为零。

AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
NSDictionary *request = @{@"email": self.email.text, @"password": self.password.text};
[manager POST:login parameters:request constructingBodyWithBlock:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
  NSLog(@"DONE!");
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    NSLog(@"Failed to log in: %@", operation.responseString);
    NSLog(@"Here's the request: %@", operation.request.HTTPBody);
}];

2 个答案:

答案 0 :(得分:4)

根据documentation

POST:parameters:constructingBodyWithBlock:success:failure

用于多部分POST请求,其默认序列化不是JSON。

  

使用多部分POST请求创建并运行AFHTTPRequestOperation。

您想要使用

POST:parameters:success:failure:

相反,如下

AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
NSDictionary *request = @{@"email": self.email.text, @"password": self.password.text};
[manager POST:login parameters:request success:^(AFHTTPRequestOperation *operation, id responseObject) {
  NSLog(@"DONE!");
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    NSLog(@"Failed to log in: %@", operation.responseString);
}];

答案 1 :(得分:0)

尝试

- (AFHTTPRequestOperation *)POST:(NSString *)URLString parameters:(NSDictionary *)parameters success:(void ( ^ ) ( AFHTTPRequestOperation *operation , id responseObject ))success failure:(void ( ^ ) ( AFHTTPRequestOperation *operation , NSError *error ))failure

此方法不需要constructBody块。我不认为该块可以是零。