【问题标题】:It shows 405 Server error at first time to get result from API server, and it works fine at second time第一次显示 405 服务器错误从 API 服务器获取结果,第二次运行正常
【发布时间】:2018-07-17 05:12:17
【问题描述】:

第一次从 API 服务器获取结果显示 405 服务器错误,第二次运行正常。我已经尝试解决这个问题一个月了,但找不到解决方案。

我使用了 Xcode 9.2、Swift 4 和 Alamofire 4.6。

主要问题 - API 服务器日志假设在 http 方法上显示“POST”。但是,它仅在 http 方法上显示“ST”。 (这是主要问题。)这导致了 405 服务器错误。

我使用 Alamofire 的源代码

Alamofire.request(url, method: .post  , parameters: [“phone”:”123123”],encoding: JSONEncoding.default)
  .validate()
  .responseJSON { (response) in
  if response.result.isSuccess {
    success(response.result.value as! Dictionary)
 } else {
 LogHelper.printLog("response : \(response)")

    fail(response.result.error)
 }
}

到目前为止我所尝试的:

  • 我在标题中添加了“application/json” => 没有任何改变。
  • 我更改了 Alamofire 版本并重新安装了它。 => 没有任何改变。
  • 参数键更改 => 405 错误和 500 错误
  • 参数值变化=>第一次405错误,第二次请求成功。
  • Alamofire 正在进行更改 => 没有任何变化。
  • 删除 Alamofire 请求中的“.validate()”。 => 没有任何改变。
  • 从 responseJSON 更改为 responseString => 没有任何变化。

【问题讨论】:

  • 您使用的网址是什么?您是否尝试过通过 Charles 或类似的方式运行您的应用程序? charlesproxy.com
  • 你应该把这个[“phone”:”123123”]改成[“phone”:“123123”]
  • @Malleswari 同样的错误..

标签: swift alamofire


【解决方案1】:

当前行为,NSURLSession 发送 Content-Length 标头而不是消息正文。

查看此链接:https://github.com/Alamofire/Alamofire/pull/2887

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-07-27
    • 2018-04-03
    • 1970-01-01
    • 2019-02-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多