ios - AFNetworking POST 作为 GET 发送

标签 ios post get afnetworking charles-proxy

如果这是正常的,请原谅,但我正在尝试使用 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/

相关文章:

javascript - 将不属于表单一部分的 HTML 页面的值传递给 PHP 页面

javascript - AngularJS JSON 始终出现在详细信息页面上

objective-c - 为什么当我想顺时针旋转时,我的旋转只能逆时针旋转?

ios - iphone 应用程序因内存压力而崩溃

ios - Swift - 隐藏和显示对象

ios - 在 Swift 中使用 Unicode 代码点

php - 通过 post [文件上传] 将表单数据提交到 iframe

javascript - 使用 javascript 在 url 中添加/修改查询字符串/GET 变量

java - 内容类型不支持 java Spring

java - Apache HTTP Client Post 似乎可以工作,但未收到变量