【问题标题】:HTTP POST "/" character problem in request body -Swift请求正文中的 HTTP POST“/”字符问题-Swift
【发布时间】:2019-08-27 12:35:36
【问题描述】:

我正在尝试快速创建一个 HTTP Post,并且在正文中有一个字符串值“djaskd/ffk”的字典。

当我更改“/”字符时它的工作,但我需要这个“/”。如何在我的 http 正文中使用“/”字符。

let session = URLSession.shared
var request = URLRequest(url:post_url)
request.httpMethod = "POST"
request.setValue("application/json; charset=utf-81", forHTTPHeaderField:"ContentType")
request.addValue("application/json" forHTTPHeaderField:"Accept")
let parameters = NSMutableDictionary()
parameters.setValue("sfala/4nv", forKey:"key")
request.httpBody = try JSONSerilization.data(withJSONObject:parameters, options .prettyPrinted)

谢谢

【问题讨论】:

  • 可能不相关但值得修复:“application/json; charset=utf-81” - application/json 上没有 charset 参数。

标签: swift http http-post


【解决方案1】:

我试过了

let session = URLSession.shared
var request = URLRequest(url: URL(string: "url")!)
request.httpMethod = "POST"
request.setValue("application/json; charset=utf-81", forHTTPHeaderField: "ContentType")
request.addValue("application/json", forHTTPHeaderField: "Accept")
let parameters = NSMutableDictionary()
parameters.setValue("sfala/4nv", forKey: "key")
let asd = try JSONSerialization.data(withJSONObject: parameters, options: .prettyPrinted)
request.httpBody = asd

print(String(data: asd, encoding: String.Encoding.utf8)!)

和控制台打印

"key" : "sfala\/4nv"

确保你做的一切都是正确的。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-28
    • 1970-01-01
    • 2011-10-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多