我有问题,我的服务器没有收到标头授权,我尝试使用此代码,但不起作用。
这是我的代码:
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")
我解决了我的问题,请阅读nex行:
let 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