401 alamofire获取标题请求

时间:2018-04-05 04:54:23

标签: swift api httprequest alamofire

我在尝试使用自定义标头发出请求时遇到错误401,当我打印标题时,它就在那里,就像我使用邮递员打电话一样,区别在于邮递员请求有效,但是在应用程序中我得到了这个错误,我不认为它是标题,因为我说他们在那里,我在这里缺少什么?

func getUserByID(completion: @escaping CompletionHandler) {
        let url_id = URL_USER_ID + String(self.userID)

        let header:HTTPHeaders = [
            "Content-Type": "application/json",
            "Authorization": "Token " + AuthService.instance.authToken
        ]

        Alamofire.request(url_id, method: .get, parameters: nil, encoding: JSONEncoding.default, headers: header).responseJSON{
            (response) in

            guard let data = response.data else {return}
            let json = try? JSON(data: data)

            if response.response?.statusCode == 200 {

                let user = json!["user"]
                let id = user["id"].stringValue
                let email = user["email"].stringValue
                let img = user["img"].stringValue
                let tipo_usuario = user["tipo_usuario"].intValue
                let nome_comp = user["nome_comp"].stringValue
                let end_cep = user["end_cep"].stringValue
                let end_logr = user["end_logr"].stringValue
                let end_num = user["end_num"].stringValue
                let end_bairro = user["end_bairro"].stringValue
                let end_complm = user["end_complm"].stringValue
                let cidade = user["cidade"]
                let cidadeID = cidade["id"].intValue
                let cidadeNome = cidade["nome"].stringValue
                let cidadeEstado = cidade["estado"].stringValue
                let telefone = user["telefone"].stringValue
                let whatsapp = user["whatsapp"].stringValue
                let pontos = user["pontos"].intValue
                let cnpj_cpf = user["cnpj_cpf"].stringValue
                let razao_social = user["razao_social"].stringValue
                let nome_fantasia = user["nome_fantasia"].stringValue
                let insc_estadual = user["insc_estadual"].stringValue
                let categoria = user["categoria"].intValue
                let nota = user["nota"].stringValue

                let cidadeUser = Cidades(id: cidadeID, nome: cidadeNome, estado: cidadeEstado)
                UserDataService.instance.setUserData(id: id, email: email, img: img, tipo_usuario: tipo_usuario, nome_comp: nome_comp, end_cep: end_cep, end_logr: end_logr, end_num: end_num, end_bairro: end_bairro, end_complm: end_complm ,cidade: cidadeUser, telefone: telefone, whatsapp: whatsapp, pontos: pontos, cnpj_cpf: cnpj_cpf, razao_social: razao_social, nome_fantasia: nome_fantasia, insc_estadual: insc_estadual, categoria: categoria, nota: nota)

                self.userEmail = email
                self.userID = Int(id)!

                completion(true)
            } else if response.response?.statusCode == 400 {
                completion(false)
            } else {
                debugPrint(response.result.error as Any)
            }
        }
    }enter code here

和错误消息:

Error=-25300, query={
    atyp = http;
    class = inet;
    "m_Limit" = "m_LimitAll";
    ptcl = http;
    "r_Attributes" = 1;
    sdmn = "api.ddns.net";
    srvr = "api.ddns.net";
    sync = syna;
}
$ curl -v \
    -H "Content-Type: application/json" \
    -H "Accept-Language: en;q=1.0, pt-BR;q=0.9" \
    -H "Authorization: Token 4e97e608c72636052583a1bb1c170485417a739b" \
    -H "User-Agent: clubelocal/1.0 (com.api; build:1; iOS 11.2.0) Alamofire/4.7.0" \
    -H "Accept-Encoding: gzip;q=1.0, compress;q=0.5" \
    "http://api.ddns.net:8000/users/25"
Optional({
  "detail" : "the user credentials was not provided."
})

1 个答案:

答案 0 :(得分:2)

我遇到了类似的问题:

邮递员成功完成了请求,但是alamofire 与401 JWT Token not found。因此响应是失败的,但是服务器对请求执行的操作就像成功一样。

在我看来,这是重定向

您应该检查它是否不是由于重定向引起的,因为默认情况下,Alamofire不会将标头中的授权字段传递给重定向的请求(在这种情况下,这似乎是合理的行为)。

因此,为了使授权可以传输,您可以这样做:

Alamofire.SessionManager.default.delegate.taskWillPerformHTTPRedirection = { session, task, response, request in
    var redirectedRequest = request

    if let originalRequest = task.originalRequest,
        let headers = originalRequest.allHTTPHeaderFields,
        let authorizationHeaderValue = headers["Authorization"]
    {
        var mutableRequest = request
        mutableRequest.setValue(authorizationHeaderValue, forHTTPHeaderField: "Authorization")
        redirectedRequest = mutableRequest
    }

    return redirectedRequest
}

//now use your requests  i.e:
Alamofire.request(url, method: .post, parameters: params, encoding: JSONEncoding.default, headers: header)
    .validate()
    .responseJSON(completionHandler: { (response) in
        switch response.result {
        case .success:
            //success
            break
        case .failure(let error):
            //fail
            break
        }
    })

如果要恢复默认行为,只需:

Alamofire.SessionManager.default.delegate.taskWillPerformHTTPRedirection = nil