【问题标题】:Alamofire POST Request with request header & body带有请求标头和正文的 Alamofire POST 请求
【发布时间】:2020-12-23 13:35:54
【问题描述】:

我正在努力使用对 API 的 POST 请求进行身份验证。

文档说我需要传入 “请求标头:内容类型:application/x-www-form-urlencoded,请求正文:client_id=CLIENT_ID&grant_type=refresh_token&refresh_token=REFRESH_TOKEN。” 我无法让它在 iOS 应用程序上运行,但尝试使用 Postman 可以正常工作。

 let headers: HTTPHeaders = [
        "Contenty-Type": "application/x-www-form-urlencoded"
    ]
    
    let params = [
    "client_id": "123",
    "refresh_token": "123"
    ]
    
    private func getAccess() {
        Alamofire.request(getRequest ,method: .post, parameters: params, headers: headers)
            .responseJSON { (response) in

            switch response.result {
            
            case .success(let json):
                print(json)
                DispatchQueue.main.async {
               }
                
            case .failure(let error):
                print(error)
            }
    }

【问题讨论】:

    标签: swift api post header alamofire


    【解决方案1】:

    您没有传递grant_type 参数。尝试将您的 params 字典更改为:

    let params = [
        "client_id": "123",
        "grant_type": "refresh_token",
        "refresh_token": "123"
    ]
    

    【讨论】:

    • 我自己想通了,但非常感谢您的回答,您完全正确
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-12-13
    • 2020-01-02
    • 2021-08-11
    • 2017-08-31
    相关资源
    最近更新 更多