【问题标题】:Swift - FAILURE: responseValidationFailed in Alamofire (StatusCode 401)Swift - 失败:Alamofire 中的 responseValidationFailed(状态码 401)
【发布时间】:2018-01-02 08:34:53
【问题描述】:

我正在使用Swift3 开发一个iOS 应用程序,并使用Alamofire 作为网络库。但是它在HTTP 请求上给responseValidationFailed 错误Status Code: 401API 和其他配置是正确的,但它给出了错误。这是我的网络电话 -

//Register user
    private func registerUser() -> Void {
        Alamofire.request(
            URL(string: "my_url")!,
            method: .post,
            parameters: ["email": email!, "gender": gender, "age":"20" ,"password": password!])
            .validate(statusCode: 200..<300)
            .responseJSON { (response) -> Void in
                print(self.CLASS_NAME+" -- registerUser()  -- response -- "+response.debugDescription)
              }

        }
    }

这是我的debugDescription -

RegistrationViewController -- registerUser()  -- response -- [Request]: POST http://beta.thesmartclubapp.com:3000/api/register/other/
[Response]: <NSHTTPURLResponse: 0x60000022f100> { URL: http://beta.thesmartclubapp.com:3000/api/register/other/ } { Status Code: 401, Headers {
    Connection =     (
        "keep-alive"
    );
    "Content-Length" =     (
        118
    );
    "Content-Type" =     (
        "application/json; charset=utf-8"
    );
    Date =     (
        "Tue, 02 Jan 2018 08:22:25 GMT"
    );
    Etag =     (
        "W/\"76-3KZXMvbalkbJsfbjY65r3dx/Mak\""
    );
    "Strict-Transport-Security" =     (
        "max-age=15552000; includeSubDomains"
    );
    Vary =     (
        Origin
    );
    "X-Content-Type-Options" =     (
        nosniff
    );
    "X-DNS-Prefetch-Control" =     (
        off
    );
    "X-Download-Options" =     (
        noopen
    );
    "X-Frame-Options" =     (
        SAMEORIGIN
    );
    "X-XSS-Protection" =     (
        "1; mode=block"
    );
} }
[Data]: 118 bytes
[Result]: FAILURE: responseValidationFailed(Alamofire.AFError.ResponseValidationFailureReason.unacceptableStatusCode(401))
[Timeline]: Timeline: { "Request Start Time": 536574145.442, "Initial Response Time": 536574146.054, "Request Completed Time": 536574146.310, "Serialization Completed Time": 536574146.310, "Latency": 0.613 secs, "Request Duration": 0.868 secs, "Serialization Duration": 0.000 secs, "Total Duration": 0.868 secs }

我在 Google 和 Stackoverflow 上进行了搜索。其他解决方案对我不起作用。 任何帮助将不胜感激。

【问题讨论】:

  • Http 代码 401 表示未经授权。看来您需要先登录。
  • 我需要在哪里登录?但我的 android 应用程序运行良好。 @YunCHEN
  • 啊……抱歉,这是注册操作!您是否在 plist 中配置选项 NSAppTransportSecurity 以允许访问 http url(不是 https)?
  • 是的,我在info.plist@YunCHEN 上有它
  • 我看到[Data]: 118 bytes,你能得到那个数据吗?也许有响应返回的自定义信息。

标签: ios swift http alamofire http-status-code-401


【解决方案1】:

您需要提供一个名称(并确认密码,检查服务设置的所有规则),因为服务需要。查看邮递员得到的结果。

更新的旧答案:删除.validate(statusCode: 200..&lt;300)是真正原因的一部分,?

【讨论】:

  • 终于成功了。我必须删除.validate(statusCode: 200..&lt;300),还必须在请求中添加encoding: JSONEncoding.default
  • 你的 API 开发者太有趣了。为什么我们需要两个布尔标志 successerror :D。如果你发送的帖子参数格式错误,它应该是Bad Request,而不是Unauthorized
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-01-18
  • 2018-07-09
  • 2021-05-18
  • 2019-06-24
  • 2016-02-07
  • 1970-01-01
  • 2020-07-05
相关资源
最近更新 更多