如何在urlRequest中将urlencoded参数添加到httpBody

时间:2019-06-27 06:23:35

标签: ios alamofire urlencode swift5 urlrequest

我为alamofire请求创建了一个通用类 我想以import tensorflow.keras.backend as K 的形式发送参数 如何将参数添加到我的aplication/x-www-form-urlencoded

我设法使用以下代码将参数urlRequest添加到urlRequest中

application/json

我需要 do { urlRequest.httpBody = try JSONSerialization.data(withJSONObject: parameters, options: []) } catch { throw AFError.parameterEncodingFailed(reason: .jsonEncodingFailed(error: error)) }

类似的东西

这是我的参数

aplication/x-www-form-urlencoded

2 个答案:

答案 0 :(得分:1)

下面是它对我迅速起作用的代码4:      let postString = "your parameter="+value+"&your parameter="+value request.httpBody = postString.data(using: .utf8) let task = URLSession.shared.dataTask(with: request, completionHandler: completionHandle) task.resume()

答案 1 :(得分:0)

尝试:

        guard let request_url = URL(string: Url) else { return }
        let parameterDictionary = ["your parameter" : value]
        var request = URLRequest(url: request_url)
        request.httpMethod = "POST"

request.setValue("Application/json", forHTTPHeaderField: "Content-Type")
request.setValue("aplication/x-www-form-urlencoded", forHTTPHeaderField: "String")
        guard let httpBody = try? JSONSerialization.data(withJSONObject: parameterDictionary, options: []) else {
            return
        }
        request.httpBody = httpBody