简体   繁体   English

AFNetworking空请求正文

[英]AFNetworking Empty Request Body

I am trying to make a simple request with the new AFNetworking 2.0 release and I cannot seem to get it to post. 我试图用新的AFNetworking 2.0版本提出一个简单的请求,我似乎无法发布它。 I get a response back from the server "Expecting text/json or application/json body" but according to the documentation on AFNetworking's GitHub page, I'm doing everything as I should be. 我从服务器“Expecting text / json或application / json body”得到了回复,但是根据AFNetworking的GitHub页面上的文档,我正在尽我所能。 It's also worth mentioning that it appears the operation.request.HTTPBody in the last line of my code always appears to be nil. 值得一提的是,似乎我的代码最后一行中的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);
}];

According to the documentation 根据文件

POST:parameters:constructingBodyWithBlock:success:failure

is for multipart POST requests and its default serialized is not JSON. 用于多部分POST请求,其默认序列化不是JSON。

Creates and runs an AFHTTPRequestOperation with a multipart POST request. 使用多部分POST请求创建并运行AFHTTPRequestOperation。

You want to use 你想用

POST:parameters:success:failure:

instead, as follows 相反,如下

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

Try 尝试

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

This method don't require the constructBody block. 此方法不需要constructBody块。 I don't think the block can be nil. 我不认为该块可以是零。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM