如果这是正常的,请原谅,但我正在尝试使用 AFNetworking 从 iOS 发送一个发布请求。使用 Charles 监控请求,我看到发送了一个 GET:
GET /api/updateTeamAlert/ HTTP/1.1
Host: www.******r.co
Accept-Encoding: gzip, deflate
Accept: */*
Accept-Language: en;q=1, fr;q=0.9, de;q=0.8, ja;q=0.7, nl;q=0.6, it;q=0.5
Connection: keep-alive
User-Agent: ****** Alerts/1.0 (iPhone Simulator; iOS 6.1; Scale/2.00)
这正常吗?我试图找出为什么我的 POST 参数在服务器上为空 - 这可能是原因吗?
我正在这样创建我的请求:
NSDictionary *params = @{@"device_id":@"test-device", @"innings":@6, @"team":@"WashingtonNationals"};
[_client postPath:@"updateTeamAlert"
parameters:params
success:^(AFHTTPRequestOperation *operation, id responseObject)
{
NSString *responseStr = [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];
NSLog(@"Request Successful, response '%@'", responseStr);
}
failure:^(AFHTTPRequestOperation *operation, NSError *error)
{
NSLog(@"[HTTPClient Error]: %@", error.localizedDescription);
}];
更新
好吧,为了让这个工作正常,我所要做的就是更改 postPath 以包含尾随的“/”——也许这对大多数人来说是显而易见的,但我希望对已接受的答案有一个解释。
最佳答案
Well, all I had to do to get this working was change the postPath to include the trailing '/' - perhaps this is obvious to most, but I would love an explanation for the accepted answer.
PHP 应用程序通常配置错误的服务器会在执行重定向时丢失信息(如 HTTP 方法)。在您的情况下,将 /
添加到您的特定 Web 服务的规范路径中,但在重定向到该端点时,POST
已更改为 GET
.
可能解决此问题的另一种方法是使用 AFURLConnectionOperation -setRedirectResponseBlock
,并确保重定向请求具有正确的动词。
关于ios - AFNetworking POST 作为 GET 发送,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17252113/