I want to append another dictionary as a parameter to httpBody
of URLRequest
Request Model:
struct RequestModel: Encodable {
let body: Body
}
struct Body: Encodable {
let name: String
let score: String
let favList: [String]
}
Api Request:
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))
}
Another dictionary: airports
var airports: [String: String] = ["YYZ": "Toronto Pearson", "DUB": "Dublin"]
trying to append airports
dictionary parameter to URLRequest
but can't append.
Appreciate your help and suggestion!
Thanks
If you have to append the airports
dictionary to the request body, you may want to include it in the Request model itself.
I would suggest updating your RequestModel
and make it Encodable
.
And include your airports
dict as a part of your RequestModel
Something like this
struct RequestModel: Encodable {
let body: Body
let airportsDict: [String:String]
}
struct Body: Encodable {
let name: String
let score: String
let favList: [String]
}
This way your httpBody
will have all the data you want to pass.
Hope this helps