This question already has an answer here:
How to send form data in POST request in Swift 3
(1个答案)
去年关门了。
如何在swift中附加带有POST请求的多个数据。
如邮递员提供的附加屏幕截图所示,当选择x-www-form-urlencoded选项时工作正常
如何使用body like'x-www-form-urlencoded'选项附加5个数据。
代码在这里,
var request = URLRequest(url: urlString)
        request.httpMethod = "POST"
        request.setValue("application/x-www-form-urlencoded;charset=UTF-8", forHTTPHeaderField: "Content-Type")

        var urlComponents = URLComponents()
        urlComponents.queryItems = [
            URLQueryItem(name: “***”, value: “***”),
            URLQueryItem(name: "***", value: "***"),
            URLQueryItem(name: "***", value: "***"),
            URLQueryItem(name: "***", value: "***"),
            URLQueryItem(name: "***", value: "***"),
        ]
        request.httpBody = urlComponents.percentEncodedQuery?.data(using: String.Encoding.utf8)

        let loadDataTask = URLSession.shared.dataTask(with: request) { (data, response, error) in
            if let _ =  error{
                completion(false,error)
            }
            else if let response = response as? HTTPURLResponse{
                if response.statusCode != 200{
                    completion(false,error)
                }
                else{
                    do{
                        if let parsedData = try? JSONSerialization.jsonObject(with: data!, options: []){
                            let ff = parsedData as? Dictionary<String,Any>
                            print(ff)
                        }
                    }
                }

            }
        }//let loadDataTask
        loadDataTask.resume()
    }

ios - 如何在POST请求中附加数据-Swift-LMLPHP

最佳答案

银行代码4:

let url = URL(string: “url”);
var urlRequest = URLRequest(url: url!)
urlRequest.setValue("application/x-www-form-urlencoded",forHTTPHeaderField: "Content-Type")
urlRequest.httpMethod = "POST"
let postString = “paramerter1=value1&parameter2=value2”
urlRequest.httpBody = postString.data(using: .utf8)

关于ios - 如何在POST请求中附加数据-Swift ,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/50115905/

10-13 05:03