简体   繁体   中英

How to print result from alamofire response using codable

New to codable and i have tried to create a class for alamofire with codable and tried to make an api request. I have getting some Swift.DecodingError.typeMismatch error and i figured it out it because of my model class. Now what i need is i want to print alamofire response in JSON(String) formate before it decoding so that i can identify the typeMismatch

static func performRequest<T:Decodable>(route:APIRouter, decoder: JSONDecoder = JSONDecoder(), completion:@escaping (Result<T,Error>)->Void) -> DataRequest {

    return AF.request(route)
        .responseDecodable (decoder: decoder){ (response: DataResponse<T>) in
            print(response.result)
            completion(response.result)
    }
}

i want some code to print the actual result from alamofire

You can print the raw Data in your responseDecodable closure by grabbing it from the DataResponse :

print(response.data.map { String(decoding: $0, as: UTF8.self) } ?? "No data.")

You can also add a separate serializer just to see the String :

.responseDecodable { }
.responseString { }

If you just want to see the response for debugging, you can debugPrint the response in the closure. This will print the request and response body datas as String s.

.responseDecodable(of: T.self) { response in
    debugPrint(response)
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM