Search code examples
iosswiftpostmannsurlsession

How to send Authorization type Bearer Token in api without library in swift 4?


I need to send Token in api call. I am getting response in postman but not in my program. Here is the code I am using to send token in api :

func getUserDetail(methodName:String,methodType:String ,finished: @escaping ((_ responseData: Data)->Void))
    {
        let url = URL(string: "\(globalURL)api/v1/\(methodName)")
        var request = URLRequest(url: url!)
        request.httpMethod = "GET"
        request.addValue(AuthTokenString, forHTTPHeaderField: "token")
        let task = URLSession.shared.dataTask(with: request) { data, response, error in
           //parsing response
        }
        task.resume()
    }

using this code I get token expired. Here is how postman calls it:

enter image description here

In postman I am getting response. I think there is something wrong in the way I am sending the token. any help.


Solution

  • Yeah, Postman abstracts away the specific details of HTTP headers associated with the different authorization schemes.

    This should do it:

    request.addValue("Bearer " + AuthTokenString, forHTTPHeaderField: "Authorization")
    

    See RFC 6750 for details on the expected format.