0

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
    }
} ```
Dávid Pásztor
  • 51,403
  • 9
  • 85
  • 116
Wilson Agene
  • 73
  • 1
  • 5
  • 1
    `try?`: NEVER USE `try` with question mark. That's just saying: You could give me an error explaining exactly why it failed? I don't want to hear about it. Use a proper `do`/`try`/`catch`. And also, see if your response is the one expected with: `print("Stringified data: \(String(data: data, encoding: .utf8)")` – Larme Jun 27 '22 at 11:40
  • Unrelated: I hope the token you've given is not private (else, remove it, and I'd go to cancel it, and redo one). Also, you can copy/paste code from console instead of screenshot, it's betterr. – Larme Jun 27 '22 at 11:41
  • See https://stackoverflow.com/questions/69002011/swift-dispatch-queue-block-not-running/69002191#69002191 for the `try?` with a little more of expllaination. Because currently, we can't say what when wrong (without testing tthe API) And share `Project` also? – Larme Jun 27 '22 at 11:57
  • For instance, I tried your code, and got response `{"timestamp":"2022-06-27T11:59:08.547+00:00","status":403,"error":"Forbidden","message":"","path":"/api/manage/user/projects"}`. But I don't if the error is on my side, or on yours, but at least, you have a JSON Response. If you have have samee, you didn't expected it I guess? – Larme Jun 27 '22 at 12:00
  • It’s a decoding error. The json you got does not match your model. – cora Jun 27 '22 at 12:01
  • can you update the question with your Project model? – mayorsanmayor Jun 27 '22 at 12:41
  • And what's the response you are getting? And what's the thrown error? – Larme Jun 27 '22 at 16:35

0 Answers0