The code below demonstrates how to extend Encodable protocol to get parameters for the HTTP GET request directly out of the swift structure. Using this way there is no need to manually create an error prone dictionary of parameters. This code also shows one of the ways how to handle HTTP errors when making data task requests using URLSession dataTask
publisher.
import UIKit
import Combine
import PlaygroundSupport
extension Encodable {
var dictionary: [String: Any]? {
guard let data = try? JSONEncoder().encode(self) else {
return nil