swift Alamofire POST应用程序/x-www-form-url编码编码

pn9klfpd  于 2022-12-28  发布在  Swift
关注(0)|答案(2)|浏览(122)

我正在使用Alamofire在Web服务上发布请求。此Web服务必须具有格式完全相同的参数“time=00:00:00”。但是,如果我将参数创建为let params = ["time":"00:00:00"],则请求将失败。当我检查HTTPBody的值时,它显示time=16%3A00%3A00。如何使请求本身使用冒号字符?

let params = ["time":"00:00:00"]
  Alamofire.request(url!, method: .post, parameters: params, encoding: URLEncoding(), headers: headers).responseJSON { response in
  // Check the http body.
  print(String(data: (response.request?.httpBody!)!, encoding: .utf8)
}
aemubtdh

aemubtdh1#

let params = ["time":"00:00:00"]
  Alamofire.request(url!, method: .post, parameters: params, encoding: URLEncoding.httpBody, headers: headers).responseJSON { response in
  // Check the http body.
  print(String(data: (response.request?.httpBody!)!, encoding: .utf8)
}

编码

smdncfj3

smdncfj32#

试试这个-

let params: [String: Any] = ["first_param": "first_param_value",  "second_param": "second_param_value"]
        let headers:HTTPHeaders=[
          "Content-Type":"application/x-www-form-urlencoded; charset=UTF-8"]
    AF.request("<ENDPOINT_URL>", method: .post, parameters: params, encoding: URLEncoding.httpBody, headers: headers).validate(statusCode: 200 ..< 299).responseData { response in
        switch response.result {
                    case .success(let data):
                        do {
                            guard let jsonObject = try JSONSerialization.jsonObject(with: data) as? [String: Any] else {
                                print("Error: Cannot convert data to JSON object")
                                return
                            }
                            guard let prettyJsonData = try? JSONSerialization.data(withJSONObject: jsonObject, options: .prettyPrinted) else {
                                print("Error: Cannot convert JSON object to Pretty JSON data")
                                return
                            }
                            guard let prettyPrintedJson = String(data: prettyJsonData, encoding: .utf8) else {
                                print("Error: Could print JSON in String")
                                return
                            }
                    
                            print(prettyPrintedJson)
                        } catch {
                            print("Error: Trying to convert JSON data to string")
                            return
                        }
                    case .failure(let error):
                        print(error)
                }
    }

相关问题