我的 API 只接受单引号包裹的 JSON。喜欢:
'{"api_key":"key_api1234","api_secret":"asdfg","uniqueid":"LDM23564GQQP","password":"test1","pin":"94729"}'
无论是在这里还是在互联网上,我都找不到明确的答案。
我在 JSON 上尝试了许多语法更改。
let parameters = ["api_key": "key_api1234",
"api_secret": "asdfg",
"uniqueid": "LDM23564GQQP",
"password": "test1",
"pin": "94729"]
guard let url = URL(string: "https://dev-api.authenticateuser") 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()
我收到 500 错误