HTTP Post 请求 API - 解析 JSON 包裹在单引号中
HTTP Post Request API - Parsing JSON wrapped in Single Quotes
我的 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 错误
在字符串中插入单引号并将其转换为数据。
问题已解决:
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")
request.addValue("*/*", forHTTPHeaderField: "Accept")
let jsonData = "'{\"api_secret\":\"asdfg\",\"uniqueid\":\"LDM23564GQQP\",\"pin\":\"94729\",\"password\":\"test1\",\"api_key\":\"key_api1234\"}'".data(using: .utf8)
request.httpBody = jsonData
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()
这是一个糟糕的设计 API。
强制将 JSON 嵌入到单引号中,如果你不能告诉后端开发人员修复它,我建议你。
不过,如果您想让它起作用,您可以将单引号转换为 Data
,然后将您称为 httpBody
的内容附加到该单引号前后。
强制展开的不良做法(使用!
),但要强调逻辑:
let parameters = ["api_key": "key_api1234",
"api_secret": "asdfg",
"uniqueid": "LDM23564GQQP",
"password": "test1",
"pin": "94729"]
var request = URLRequest(url: URL(string: "www.whosebug.com")!)
let singleQuote = "'".data(using: .utf8)!
let parametersJSON = try! JSONSerialization.data(withJSONObject: parameters, options: [])
let finalBody = singleQuote + parametersJSON + singleQuote
print("request.httpBody string: \(String(data: finalBody, encoding: .utf8)!)")
request.httpBody = finalBody
输出:
$>request.httpBody string: '{"api_key":"key_api1234","uniqueid":"LDM23564GQQP","pin":"94729","password":"test1","api_secret":"asdfg"}'
我的 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 错误
在字符串中插入单引号并将其转换为数据。
问题已解决:
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")
request.addValue("*/*", forHTTPHeaderField: "Accept")
let jsonData = "'{\"api_secret\":\"asdfg\",\"uniqueid\":\"LDM23564GQQP\",\"pin\":\"94729\",\"password\":\"test1\",\"api_key\":\"key_api1234\"}'".data(using: .utf8)
request.httpBody = jsonData
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()
这是一个糟糕的设计 API。 强制将 JSON 嵌入到单引号中,如果你不能告诉后端开发人员修复它,我建议你。
不过,如果您想让它起作用,您可以将单引号转换为 Data
,然后将您称为 httpBody
的内容附加到该单引号前后。
强制展开的不良做法(使用!
),但要强调逻辑:
let parameters = ["api_key": "key_api1234",
"api_secret": "asdfg",
"uniqueid": "LDM23564GQQP",
"password": "test1",
"pin": "94729"]
var request = URLRequest(url: URL(string: "www.whosebug.com")!)
let singleQuote = "'".data(using: .utf8)!
let parametersJSON = try! JSONSerialization.data(withJSONObject: parameters, options: [])
let finalBody = singleQuote + parametersJSON + singleQuote
print("request.httpBody string: \(String(data: finalBody, encoding: .utf8)!)")
request.httpBody = finalBody
输出:
$>request.httpBody string: '{"api_key":"key_api1234","uniqueid":"LDM23564GQQP","pin":"94729","password":"test1","api_secret":"asdfg"}'