Problem with GET-Request (NSURLErrorDomain: -1003)

I face some issues performing a simple GET-Request.

I defined a Struct whose variables are named equally to the API:

struct CanteenItem: Codable {
    var id: Int
    var name: String
    ...
}

I then created a variable in my view:

@State var canteens = [CanteenItem]()

Also in my view, I created a function for fetching the data:

func fetchData() async {
        guard let url = URL(string: "http://localhost:8080/canteens") else {
            print("Invalid URL")
            return
        }
        // fetch data
        do {
            let(data, _) = try await URLSession.shared.data(from: url)
            
            // decode
            if let decodedResponse = try? JSONDecoder().decode([CanteenItem].self, from: data){
                canteens = decodedResponse
                print(decodedResponse)
            }
        } catch {
            print("Request unsucessful")
        }
    }

I now call the function like this:

ScrollView(.vertical, showsIndicators: false){
    VStack(spacing: 0){        
        ForEach(canteens, id: \.id) { item in
            Button {
                ...
            } label: {
                ...
            }
            .buttonStyle(ScaledButtonStyle())
            .opacity(globalStore.showDetailView ? (globalStore.currentCanteen?.id == item.id ? 1 : 0) : 1)
        }
    }
}
.task {
    await fetchData()
}

Executing results in the following error:

nw_proxy_resolver_create_parsed_array [C1.1 proxy pac] Evaluation error: NSURLErrorDomain: -1003

Error -1003 is cannotFindHost. Most likely you're blocked by ATS (Cocoa Keys). Also, I suggest you print the full error in your catch rather than just "Request unsuccessful".

1 Like

That solved it, thanks! For development purposes, in my Info.plist, under App Transport Security Settings I set Allow Arbitrary Loads and Allows local networking to YES and now it works!

This might also be interesting for someone with a similar problem: