I have bunch of json string that need to format as payload for different request api call. I have no problem to get correct response with following code
func request<T: Codable>(url: String, method: String = "POST", body: Encodable? = nil, session: URLSession = URLSession.shared) -> T? {
...
return response
}
struct Student: Codable {
var name: String
var skill: String
}
struct Response: Codable {
let status: String
}
struct Student: Codable {
var name: String
var skill: String
}
let rawData: String = "{\"name\": \"withSkill\", \"skill\": \"yell\"}"
var data: Student = try! JSONEncoder().encode(rawData)
let response = request(url: "http:localhost", body: data, session: session) /// Response(status: 200)
However if I have different payload and response with all different nested json format like following, does it means I need to all boiler plate code for swift codable protocol? or I have different approach?
struct Student: Codable {
var name: String
var skill: String
}
struct StudentWithAddress: Codable {
var name: String
var skill: String
var address: StudentAddress
}
struct StudentAddress: Codable {
let zipcode: String
let address: String
let city: String
let state: String
}
struct StudentWithAddressResponse: Codable {
let status: String
let exist: String
}