401 alamofire 获取带有标头的请求

Posted

技术标签:

【中文标题】401 alamofire 获取带有标头的请求【英文标题】:401 alamofire get request with headers 【发布时间】:2018-04-05 04:54:22 【问题描述】:

我在尝试使用自定义标头发出请求时遇到错误 401,当我打印标头时,它就在那里,就像我使用邮递员拨打电话一样,不同之处在于在邮递员中请求有效,但在app 我得到那个错误,我不认为它是标题,因为正如我所说的那样,它们在那里,我在这里缺少什么?

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."
)

【问题讨论】:

您正在使用form-dataform-urlencoded 在邮递员中调用Web 服务? error 401 表示授权问题,授权时也要传递用户名和密码 HTTP 401 表示请求未经授权。您还可以在detail 中看到the user credentials was not provided。我假设授权方法是令牌,所以请查看 API 文档。应该是Token 吗?更常见的方案是BasicBearer 端点使用什么形式的授权?检查iana schemes 它是“令牌”,该 api 是使用 django-rest 制作的,它可以与邮递员请求一起正常工作 【参考方案1】:

我遇到了类似的问题:

邮递员成功执行请求,但 alamofire 与401JWT 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

【讨论】:

以上是关于401 alamofire 获取带有标头的请求的主要内容,如果未能解决你的问题,请参考以下文章

Alamofire - 带有自定义标头的 Rest API 调用

React - 带有标头的 ajax GET 请求仍返回 401(未经授权)

alamofire 499 使用标头和参数发出获取请求时

alamofire:带有标头的 POST 请求

带有标头的 Alamofire POST 请求

带有标头的 Alamofire 请求?