Swift 4 - 发送application / x-www-form-urlencoded JSON

时间:2018-05-15 20:28:18

标签: json swift

我想将我的JSON编码为application / x-www-form-urlencoded

var req = URLRequest(url: url)
req.httpMethod = "POST"
req = JsonHelper.defineCommonHeaders(request: req)
var headers = req.allHTTPHeaderFields ?? [:]
headers["Content-Type"] = "application/x-www-form-urlencoded"
print(headers)

do {
    let jsonData = try encoder.encode(self)
    let jsonString = String(data: jsonData, encoding: .utf8)!
    req.httpBody = jsonData as Data
    print ("httpBody is: ", jsonString)
} catch {
    //TODO:error handling
}

从Postman发送我的JSON很好,但从Swift 4发送不起作用。 我想我需要在某处编码空键。有很好的例子POST request using application/x-www-form-urlencoded 但它没有涵盖如何在Swift 4中做到这一点。我需要改变什么?

0 个答案:

没有答案