Make a Get Request api to a JWT enabled endpoint

134 Views Asked by At

I am trying to make a get request to a JWT enabled API but I am getting my result in bytes instead of objects

after the user logs in it is expected to go the next page where it lists user projects, at the moment I've created a button which when clicked, should send a Get Request with the JWT token received from the API function below.

I have tried:

    func getUserProject(token: String, completion: @escaping (Result<[Project], NetworkError>) -> Void) {
        
        guard let url = URL(string: "https://api.t.io/api/manage/user/projects?archived=false&logic=and&page=0&size=10") else {
            completion(.failure(.invalidURL))
            print("unable to connect URL")
            return
        }
        var request = URLRequest(url: url)
        request.httpMethod = "GET"
        request.addValue("Bearer \(token)", forHTTPHeaderField: "Authorization")
      let task =  URLSession.shared.dataTask(with: request) { (data, response, error) in
            guard let data = data, error == nil else {
                completion(.failure(.noData))
                print("Bad")
                return
            }
            guard let Projects = try? JSONDecoder().decode([Project].self, from: data) else {
                completion(.failure(.decodingError))
                print(data)
                print("Error Decoding Data")
                return
            }
            completion(.success(Projects))
        }
            task.resume()
    }

This is the error I am getting from my logs:

enter image description here

this is my project model:

    let createdBy: String
    let createdDate: EdDate
    let lastModifiedBy: String
    let lastModifiedDate: EdDate
    let id: Int
    let name: String
    let customID: JSONNull?
    let shared: Bool
    let salt: JSONNull?
    let isCOMRequestAutomatic: Bool
    let client: Client
    let company: Company
    let projectKeywords: [JSONAny]
    let projectStartDate, projectEndDate: JSONNull?

    enum CodingKeys: String, CodingKey {
        case createdBy, createdDate, lastModifiedBy, lastModifiedDate, id, name, status
        case imageURL
        case customID
        case shared, salt
        case isCOMRequestAutomatic
        case client, company, projectKeywords, projectStartDate, projectEndDate
    }
} ```
0

There are 0 best solutions below