hi CWC team,
I’m trying to Make a GET API call but I’m getting an error that I cannot find the fix in google,
this works perfectly in the module 6 (yelp app) but not working for my call for some reason.
has anybody faced this issue? if so, how did you fix it?
thanks
Here is my viewModel
//
// ContentModel.swift
// Secret
//
// Created by Nat-Serrano on 8/11/21.
//
import Foundation
class ContentModel: ObservableObject, Identifiable {
@Published var faces = [Face]() //to host the data pulled from the api FACEDETECT PIXLAB
//func face detect
func faceDetect(urlPath: String){
//create url
let urlString = Constants.pixLabapiURL + "facedetect?img=\(urlPath)"
let url = URL(string: urlString)
//create URL request (GET)
if let url = url { //to avoid it being empty
// Create URL Request
var request = URLRequest(url: url, cachePolicy: .reloadIgnoringLocalCacheData, timeoutInterval: 10.0)
request.httpMethod = "GET"
request.addValue(Constants.pixLabApiKey, forHTTPHeaderField: "Authorization")
// Get URLSession
let session = URLSession.shared
// Create Data Task
let dataTask = session.dataTask(with: request) { (data, response, error) in
// Check that there isn't an error
if error == nil {
do {
let decoder = JSONDecoder()
let result = try decoder.decode(faceDetect.self, from: data!)
DispatchQueue.main.async { //assign things to properties from a background thread.NO GOOD PRACTICE use
self.faces = result
}
}
catch {
print(error) //error from do block
}
}
}
}
}
//func mogrify
//func nsfw
}
here is my model
import Foundation
struct Face: Decodable {
var face_id: Int
var bottom: Int
var right: Int
var top: Int
var left: Int
var width: Int
var height: Int
}
struct faceDetect: Decodable {
var faces = [Face]() //response is an array of faces
}
this is the api
https://pixlab.io/cmd?id=facedetect