I have the token in authorization Type Bearer Token in URLSession
I always use alamofire and swiftjson
Unfortunately I searched a lot and couldn't find the right way in URLSession
I have this token : 28|cSTxxxxxxxxxxxxxx Example
But it gives nil
struct model : Codable, Identifiable {
let id = UUID()
var name : String
}
class modelapiClass : ObservableObject {
@Published var modelBib : [model] = []
func getData() {
guard let url = URL(string: "https://xxxxxxxxxx") else {
return
}
let token = "28|cSTxxxxxxxxxxxxxx" Example
var request = URLRequest(url: url)
request.setValue( token, forHTTPHeaderField: "Authorization")
let uelSession = URLSession.shared.dataTask(with: url) { data, _, _ in
do {
let dataModel = try JSONDecoder().decode([model].self, from: data!)
print(dataModel)
DispatchQueue.main.async {
self.modelBib = dataModel
}
} catch _ {
}
}
uelSession.resume()
}
}
CodePudding user response:
try using this (in addition to vadian comment):
request.setValue("Bearer \(token)", forHTTPHeaderField: "Authorization")