我對Swift非常陌生,在我的應用程序中,我正在嘗試執行POST請求。在swift 3中的多個參數的HTTP POST請求
網址看起來像下面
下面是我的代碼
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()
我收到以下錯誤
錯誤域= NSCocoaErrorDomain代碼= 3840「JSON文本沒有從數組或對象開始並選擇允許片段未設置的選項。「
任何人都可以幫助我在代碼中做錯了什麼,我怕我沒有正確地發送多個參數。
如果可能的話,我會很感激你的答案。
感謝您的答覆。我剛剛編輯了我的問題,糾正了重複的參數問題後,我得到了同樣的錯誤。 –
你的'/ preset /'的值不是'String',所以不應該編譯。試試這個let參數= [「/ uid /」:「12234353」,「/ hash /」:「9530cf040a49fba786100a44f913ad25」,「/ preset /」:「986tzugh76rtzu00fg」] Dictionary' –
非常感謝回覆,請你看看我的評論上面的其他答案,我真的很感謝一些幫助 –