'I want to use the Token in URLSession with SwiftUI
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()
}
}
Solution 1:[1]
try using this (in addition to vadian comment):
request.setValue("Bearer \(token)", forHTTPHeaderField: "Authorization")
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
