状态代码 405 问题..Alamofire POST 请求...

status code 405 issue..Alamofire POST request...

我正在尝试发出 Alamofire POST 请求。这就是我提出请求的方式..

     Alamofire.request(url, method: .post, parameters: parameters, encoding: URLEncoding(destination: .queryString), headers : headers)
        .responseString { response in
            print(response.result)
 }

虽然我得到的结果是 'SUCCESS',但状态代码始终显示为 405,而它应该是 200。在请求的 'encoding' 部分,我尝试了所有类似的方法JSONEncoding.default、JSONEncoding.prettyPrinted、URLEncoding.httpbody...但状态码始终是 405。有人可以帮忙吗?提前致谢...

尝试替换:

responseStringresponseJSON AND URLEncoding(destination: .queryString)URLEncoding.default

喜欢:

Alamofire.request(strURL, method: .post, parameters: params, encoding: URLEncoding.default, headers: headers).responseJSON { (responseObject) -> Void in 

//Do something

}

我认为问题出在您的服务器上,因为此状态代码仅在服务器禁用 api

时出现

The HTTP 405 Method Not Allowed response status code indicates that the request method is known by the server but has been disabled and cannot be used. The two mandatory methods, GET and HEAD, must never be disabled and should not return this error code.

所以请联系您的服务器(后端开发人员),确保您的url是正确的

这是这个问题的解决方案...必须进行一些更改...

给出的 header 是这样的:让 headers = [ "Content-Type" : "application/json"]。但不得不让headers = ["Content-Type":"application/x-www-form-urlencoded"]。 编码也应为 URLEncoding.httpBody。

进行这些更改使其工作正常...

我遇到了同样的问题。 API 端点在 Android Retrofit 上运行良好,也通过 PostMan 进行了测试。 header 的 Content-Type 也是 application/json。 这真是一个奇怪的错误。我用 Fiddler 来检查响应。 错误消息是

The requested resource does not support http method 'T'/'ST'.

我用了GET/POST方法,但是它说我用的是T/ST,而不是GET/POST

我从 Alamofire 的问题中找到了答案。

当我调用不带参数的 API 端点时,我使用空白字典作为参数。

Alamofire.request("URL", method: .get, parameters: [:], encoding: JSONEncoding.default).responseString(completionHandler: completionHandler)

我改成了无

Alamofire.request("URL", method: .get, parameters: nil, encoding: JSONEncoding.default).responseString(completionHandler: completionHandler)

之后,一切正常。 希望它能帮助你。干杯!