这是我的代码:
let headers = [ "authorization": "token abcd"]var request = URLRequest.init(url: NSURL(string: "http://127.0.0.1:7000/API/channels?filter=contributed")! as URL)request.httpMethod = "GET"request.allhttpheaderFIElds = headerslet config = URLSessionConfiguration.defaultconfig.httpAdditionalheaders = headerslet session = URLSession.init(configuration: config)let dataTask = session.dataTask(with: request,completionHandler: { (data,response,error) -> VoID in if (error != nil) { print(error ?? "") } else { let httpResponse = response as? httpURLResponse print(httpResponse ?? "") }})
正如您所看到的,我尝试在会话配置和请求中设置令牌,但没有一个正在工作.
解决方法 这似乎有效:// Set the security headerprivate var credentials: String { return "\(participantID):\(password)"}private var basicAuthheader: String { return "Basic \(credentials)"}func getSettings(participantID: Int,password: String) -> Bool { self.participantID = participantID self.password = password let path = "/settings/\(participantID)" guard let url = URL(string: "\(BASE_URL)\(path)") else { Log.e("InvalID URL string,Could not convert to URL") return false } var urlRequest = URLRequest(url: url) urlRequest.setValue("application/Json",forhttpheaderFIEld: "Content-Type") urlRequest.setValue(basicAuthheader,forhttpheaderFIEld: "Authorization") urlRequest.setValue(APP_file_name,forhttpheaderFIEld: "User-Agent") // This is a synchronous wrapper extension around URLSession.dataTask() let (data,error) = URLSession.shared.synchronousDataTask(with: urlRequest) // Process the result...}
注意:我的同事写的代码.谢谢约翰!
总结以上是内存溢出为你收集整理的ios – URLSession没有在标题swift 4中传递’Authorization’键全部内容,希望文章能够帮你解决ios – URLSession没有在标题swift 4中传递’Authorization’键所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)