【问题标题】:How do I attach data in POST request - Swift [duplicate]如何在 POST 请求中附加数据 - Swift [重复]
【发布时间】:2018-10-11 10:58:43
【问题描述】:

如何通过 POST 请求快速附加多个数据。 作为邮递员的附加屏幕截图,选择 x-www-form-urlencoded 选项时工作正常 如何使用'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()
    }

【问题讨论】:

  • 仅字符串数据
  • **最简单的解决方案:**在邮递员(附加屏幕)中单击Code。在单击 Code 后出现的模式中选择 Swift。你会得到你需要的代码,去掉不必要的邮递员参数

标签: ios swift api post


【解决方案1】:

SWIFT 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)

【讨论】:

    【解决方案2】:

    创建 http 正文的一种简单方法是像这样使用URLComponents

    var request = URLRequest(url: yourUrl)
    request.httpMethod = "POST"
    request.setValue("application/x-www-form-urlencoded;charset=UTF-8", forHTTPHeaderField: "Content-Type")
    
    var urlComponents = URLComponents()
    urlComponents.queryItems = [
        URLQueryItem(name: "your_first_parameter", value: "someValue"),
        URLQueryItem(name: "your_second_parameter", value: "someValue"),
        URLQueryItem(name: "your_third_parameter", value: "someValue")
    ]
    request.httpBody = urlComponents.percentEncodedQuery?.data(using: .utf8)
    

    【讨论】:

    • 我已经更新了代码。它不工作。我得到了响应代码 400。
    • 您是否正确设置了查询项?因为否则与您接受的解决方案没有什么不同。除了这个解决方案在查询编码方面更健壮。
    猜你喜欢
    • 2022-01-11
    • 2020-04-18
    • 2021-04-26
    • 2014-03-02
    • 2016-11-16
    • 2018-03-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多