Alamofire-带JSON的POST请求给301

时间:2018-07-05 18:55:07

标签: json swift alamofire swift4

我想使用JSON发送POST请求,我期望JSON对象带有auth_token和refresh_token作为响应。我的代码如下:

let headers: HTTPHeaders = [
    "Authorization": "Basic " + data.toBase64() //data is just string needed for authentication
]

// this should be a JSON
let parameters = [
    "code": self.code,
    "grant_type": "authorization_code",
    "redirect_uri": "populi://callback"
]

Alamofire.request("https://populi.pl/api/v1/auth/token", method: .post, parameters: parameters, encoding: JSONEncoding.default, headers: headers).responseString { response in
    print(response.request)
    print("\n3 RESPONSE : \(response)")
    print("\n3 POST : \(response.response)")
    print("\n3 DATA : \(response.data)")
    print("\n3 RESPONSE RESULT : \(response.result)")
    print("\n3 RESPONSE RESULT VALUE : \(response.result.value)")
    print("3 ERROR : \(response.error)")
}

是的,我打印了很多东西,这是因为我是网络新手。 作为回应,我将永久性地迁移301。为什么?我不知道怎么了这是参数中的错误JSON还是带有重定向功能?我应该得到200和JSON作为响应。

2 个答案:

答案 0 :(得分:0)

尝试

func getData(){
 let todoEndpoint: String = Your url
        let params: Parameters = [yor parameters]
        let headers: [String : String] = [
            "Content-Type": "application/x-www-form-urlencoded",
        ]
 Alamofire.request(todoEndpoint, method: .post, parameters: params,headers: headers)
            .responseJSON { response in
                switch response.result {
                case .success:
                    let json = JSON(response.result.value as Any)
                    print(json)

                    break
                case .failure(let error):

                    print(error)
                }
        }
}

答案 1 :(得分:0)

我在请求网址末尾用“ /”解决了这个问题。