我是Swift的新手,在我的应用中我正在尝试发出POST请求。
Url如下所示
以下是我的代码
let parameters = ["/uid/": "12234353", "/hash/": "9530cf040a49fba786100a44f913ad25", "/preset/":"986tzugh76rtzu00fg"]as Dictionary<String, String>
guard let url = URL(string: "https://example.com/login/auto/") else { return }
var request = URLRequest(url: url)
request.httpMethod = "POST"
request.addValue("application/json", forHTTPHeaderField: "Content-Type")
guard let httpBody = try? JSONSerialization.data(withJSONObject: parameters, options: []) else { return }
request.httpBody = httpBody
let session = URLSession.shared
session.dataTask(with: request) { (data, response, error) in
if let response = response {
print(response)
}
if let data = data {
do {
let json = try JSONSerialization.jsonObject(with: data, options: [])
print(json)
} catch {
print(error)
}
}
}.resume()
我收到以下错误
Error Domain = NSCocoaErrorDomain Code = 3840“JSON文本没有以数组或对象开头,并且选项允许未设置片段。”
任何人都可以帮我解决我在代码中做错的事情,恐怕我没有正确发送多个参数。
如果可能的话,我将通过示例感谢答案。
答案 0 :(得分:0)
此错误可能是因为参数中存在重复项。字典键应该是唯一的。
在let parameters = ["/uid/": "12234353", "/hash/": "9530cf040a49fba786100a44f913ad25", "/hash/":986tzugh76rtzu00fg]as Dictionary<String, String>
中,您有"\hash\"
的2个实例。另外,"/hash/":986tzugh76rtzu00fg]as
的类型可能会给您一个错误,因为它应该是String
。
试试这段代码:
let parameters = ["/uid/": "12234353", "/hash/": "9530cf040a49fba786100a44f913ad25", "/preset/":"986tzugh76rtzu00fg"] as Dictionary<String, String>
guard let url = URL(string: "https://example.com/login/auto/") else { return }
var request = URLRequest(url: url)
request.httpMethod = "POST"
request.addValue("application/json", forHTTPHeaderField: "Content-Type")
guard let httpBody = try? JSONSerialization.data(withJSONObject: parameters, options: []) else { return }
request.httpBody = httpBody
let session = URLSession.shared
session.dataTask(with: request) { (data, response, error) in
if let response = response {
print(response)
}
if let data = data {
do {
let json = try JSONSerialization.jsonObject(with: data, options: [])
print(json)
} catch {
print(error)
}
}
}.resume()
答案 1 :(得分:0)
确保正确设置了httpBody。使用上面的确切代码,我得到3个错误,我需要修复。
一旦我完成了这个,我得到了这个作为httpBody,它应该工作。
let parameters = ["uid": "12234353", "hash": "9530cf040a49fba786100a44f913ad25"] as Dictionary<String, String>
let httpBody = try? JSONSerialization.data(withJSONObject: parameters, options: [])
let string = String(data: httpBody!, encoding: String.Encoding.utf8)
print(string)
Optional("{\"hash\":\"9530cf040a49fba786100a44f913ad25\",\"uid\":\"12234353\"}")
更新根据以下对话。用户需要使用这些参数生成URL而不是使用标题
let baseUrl = "https://example.com/login/auto"
let parameters = ["uid": "12234353", "hash": "9530cf040a49fba786100a44f913ad25"] as Dictionary<String, String>
var queryString = ""
for (key, value) in parameters {
queryString += "/\(key)/\(value)"
}
print(queryString)
// output /hash/9530cf040a49fba786100a44f913ad25/uid/12234353
print("\(baseUrl)\(queryString)")
// output https://example.com/login/auto/hash/9530cf040a49fba786100a44f913ad25/uid/12234353
<强>更新强>
要使参数动态化,只需创建一个空字典并根据需要添加键和值
let baseUrl = "https://example.com/login/auto"
var parameters = [String:String]()
parameters["uid"] = "12234353"
parameters["hash"] = "9530cf040a49fba786100a44f913ad25"
var queryString = ""
for (key, value) in parameters {
queryString += "/\(key)/\(value)"
}
let urlString = "\(baseUrl)\(queryString)"
let url = URL(string: urlString)