将多个数据附加到 URLRequest 的 httpbody
Append multiple data to httpbody of URLRequest
我想将另一个字典作为参数附加到 URLRequest
的 httpBody
请求型号:
struct RequestModel: Encodable {
let body: Body
}
struct Body: Encodable {
let name: String
let score: String
let favList: [String]
}
Api 请求:
do {
var urlRequest = URLRequest(url: resourceURL)
urlRequest.httpMethod = kHTTPMethodPOST
urlRequest.addValue("application/json", forHTTPHeaderField: "Content-Type")
urlRequest.httpBody = try JSONEncoder().encode(self.requestModel)
let dataTask = URLSession.shared.dataTask(with: urlRequest) { data, response, error in
guard let httpResponse = response as? HTTPURLResponse,
httpResponse.statusCode == 200,
let jsonData = data else {
completion(.failure(.responseError))
return
}
}
dataTask.resume()
} catch {
completion(.failure(.unknownError))
}
另一个字典:airports
var airports: [String: String] = ["YYZ": "Toronto Pearson", "DUB": "Dublin"]
尝试将 airports
字典参数附加到 URLRequest
但无法附加。
感谢您的帮助和建议!
谢谢
POST
JSON 的常用语法是
do {
var urlRequest = URLRequest(url: resourceURL)
urlRequest.httpMethod = "POST"
let postData = try JSONEncoder().encode(self.requestModel)
urlRequest.httpBody = postData
urlRequest.setValue("\(postData.count)", forHTTPHeaderField:"Content-Length")
urlRequest.setValue("application/json", forHTTPHeaderField:"Accept")
urlRequest.setValue("application/json", forHTTPHeaderField:"Content-Type")
}
如果您必须将 airports
字典附加到请求正文,您可能希望将其包含在请求模型本身中。
我建议更新您的 RequestModel
并使其成为 Encodable
。
并将您的 airports
口述作为 RequestModel
的一部分
像这样
struct RequestModel: Encodable {
let body: Body
let airportsDict: [String:String]
}
struct Body: Encodable {
let name: String
let score: String
let favList: [String]
}
这样您的 httpBody
将拥有您要传递的所有数据。
希望对您有所帮助
我想将另一个字典作为参数附加到 URLRequest
的 httpBody
请求型号:
struct RequestModel: Encodable {
let body: Body
}
struct Body: Encodable {
let name: String
let score: String
let favList: [String]
}
Api 请求:
do {
var urlRequest = URLRequest(url: resourceURL)
urlRequest.httpMethod = kHTTPMethodPOST
urlRequest.addValue("application/json", forHTTPHeaderField: "Content-Type")
urlRequest.httpBody = try JSONEncoder().encode(self.requestModel)
let dataTask = URLSession.shared.dataTask(with: urlRequest) { data, response, error in
guard let httpResponse = response as? HTTPURLResponse,
httpResponse.statusCode == 200,
let jsonData = data else {
completion(.failure(.responseError))
return
}
}
dataTask.resume()
} catch {
completion(.failure(.unknownError))
}
另一个字典:airports
var airports: [String: String] = ["YYZ": "Toronto Pearson", "DUB": "Dublin"]
尝试将 airports
字典参数附加到 URLRequest
但无法附加。
感谢您的帮助和建议!
谢谢
POST
JSON 的常用语法是
do {
var urlRequest = URLRequest(url: resourceURL)
urlRequest.httpMethod = "POST"
let postData = try JSONEncoder().encode(self.requestModel)
urlRequest.httpBody = postData
urlRequest.setValue("\(postData.count)", forHTTPHeaderField:"Content-Length")
urlRequest.setValue("application/json", forHTTPHeaderField:"Accept")
urlRequest.setValue("application/json", forHTTPHeaderField:"Content-Type")
}
如果您必须将 airports
字典附加到请求正文,您可能希望将其包含在请求模型本身中。
我建议更新您的 RequestModel
并使其成为 Encodable
。
并将您的 airports
口述作为 RequestModel
像这样
struct RequestModel: Encodable {
let body: Body
let airportsDict: [String:String]
}
struct Body: Encodable {
let name: String
let score: String
let favList: [String]
}
这样您的 httpBody
将拥有您要传递的所有数据。
希望对您有所帮助