【发布时间】:2018-07-13 10:05:32
【问题描述】:
我需要发出一个 post 请求以在 post 请求的正文中发送一个字典,该字典需要用 utf-8 编码, 我在下面尝试了这个,但它给出了一个错误:
let DicObject: NSMutableDictionary = NSMutableDictionary()
DicObject.setValue("cf", forKey: "a")
DicObject.setValue("", forKey: "scs")
DicObject.setValue("Uploads/" + nameOfFile, forKey:"p")
当我尝试制作时
request.httpMethod = "POST"
// request.httpBody = jsonObject as Data
request.addValue("Content-Type", forHTTPHeaderField: "application/x-www-form-urlencoded; charset=utf-8")
不起作用,有谁知道如何将jsonObject编码转换为utf-8
谢谢。
【问题讨论】:
-
将您的 Json 对象转换为字符串并传入 POST 请求。
-
它不起作用,错误消息:无法将类型“NSMutableDictionary”的值强制转换为类型“NSString”。并尝试这样: let convert = (DicObject as NSString).utf8String
-
请试试我的答案!