在swift 3中发送授权头的错误不起作用,(服务器没有收到授权头)

时间:2018-03-22 22:59:08

标签: swift web-services token

我有问题,我的服务器没有收到标题授权, 我尝试使用此代码但不起作用。

这是我的代码:

let token: String? = "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpYXQiOjE1MjE3NDc3OTYsImV4cCI6MTUyNDMzOTc5NiwiZGF0YSI6eyJpZCI6IjM2IiwiaWRfZW1wcmVzYSI6IjEiLCJub21icmVzIjoiU2VyZ2lvIEFsZWphbmRybyIsImFwZWxsaWRvX3BhdGVybm8iOiJSb3NhZG8iLCJhcGVsbGlkb19tYXRlcm5vIjoiQXp1bCIsInpvbmFfaG9yYXJpYSI6IkFtZXJpY2FcL01lcmlkYSIsImlkX3BlcmZpbCI6IjEiLCJjb3JyZW8iOiJydXNzZWxhbGV4aXMxMjNAZ21hLmNvbSIsInVzZXJuYW1lIjoiZW1wbGVhZG9kZW1vIiwicGFzc3dkIjoiKkE0QjYxNTczMTkwMzg3MjRFMzU2MDg5NEY3RjkzMkM4ODg2RUJGQ0YiLCJhY3Rpdm8iOiIxIiwiZmhfY2FwdHVyYSI6IjIwMTctMDItMjAgMjE6MDI6NTUiLCJkb21pY2lsaW8iOiIiLCJjcCI6IiIsInRlbGVmb25vIjoiIiwidXN1YXJpbyI6IjEiLCJpcCI6IjE4OS4xNTAuMTQxLjExNyJ9fQ.79HJPR04IjHBwLzpeUnjJel0UAYSG0rtqPvOPca7Uds"

guard let name = token, !name.isEmpty else { return }

let url = URL(string: "http://dev.viupruebas.com.mx/webservice/test/getvehiculos")
var request = NSMutableURLRequest(url: url!)
request.httpMethod = "GET"
request.setValue("Bearer \(name)", forHTTPHeaderField: "Authorization")
request.setValue("application/x-www-form-urlencoded", forHTTPHeaderField: "Content-Type")

1 个答案:

答案 0 :(得分:0)

我解决了我的问题,请阅读nex专栏:

  

让task = URLSession.shared.dataTask(with:request){data,response,   

中的错误

我的最终代码是:

let token: String? = "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpYXQiOjE1MjE4MjY2MTgsImV4cCI6MTUyNDQxODYxOCwiZGidXN1YXJpbyI6IjEiLCJpcCI6IjE4Ny4xNTUuOTIuMjEzIn19.pu6_1LmfxliXke_WMY82hEKy0Dsn1fk-feaTKnfGpbo"

              guard let name = token, !name.isEmpty else { return }
        //url
        let url = URL(string: "http:/webservice")!

        var request = URLRequest(url: url)

        request.addValue("\(name)", forHTTPHeaderField: "Authorization")

        request.httpMethod = "GET"

        let task = URLSession.shared.dataTask(with: request) { data, response, error in