I am trying to pass access token in Alamofire
but getting confuse in various methods around web.
Below are methods which we need to use.
let todosEndpoint: String = "https:url......."
let headers = [
"Authorization": "Bearer \(token!)",
"Content-Type": "application/X-Access-Token"
]
let Auth_header = [ "Authorization" : tokenString! ]
Alamofire.request(todosEndpoint, method: .get, parameters: nil, encoding: JSONEncoding.default, headers: Auth_header)
.responseJSON { response in
print("response.request \(response.request)") // original URL request
print("response.response \(response.response)") // HTTP URL response
print("response.data \(response.data)") // server data
print("response.result \(response.result)")
print("response \(response)")
}
}
OR
let aManager = SessionManager()
aManager.session.configuration.httpAdditionalHeaders = [
"Authorization": "Bearer tokenString"]
OR
let headerss = [
"Authorization": tokenString]
OR
let aManager = SessionManager()
aManager.session.configuration.httpAdditionalHeaders = [
"Authorization": "Basic tokenString"]
What is proper way to pass access token?
I'm using look like this :-
OR
Did you tried this, it's available in Alamofire documentation:
Another example is:
One more way is:
It can be done by using Alamofire in following way:
If you are using Alamofire 4.0 or greater, you can use
RequestAdapter
protocol to intercept the request and inject the JWT token. This solution is perfect if you make many requests and have to use JWT in each of them.Somewhere in the class you initialize the
SessionManager
like this:And you use it whenever you want to: