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