1

I am using the following code to get data from an URL and later show it on swift. but i have the question if is possible to add Headers to the URL?

static private func createPDFLocalURL(urlString:String, fileName:String) -> FileManagementResponse {
   let url = URL(string: "http://www.africau.edu/images/default/sample.pdf")
   let pdfData = try? Data.init(contentsOf: url!)
//....
}
Ricardo Guerrero
  • 433
  • 1
  • 5
  • 21
  • 3
    You don’t add headers to a `URL`, but rather create a `URLRequest` from the `URL` and the set the headers for the request (which you submit via `URLSession`, a nice asynchronous way to submit requests). The `Data(contentsOf:)` is generally discouraged, anyway. – Rob Aug 26 '21 at 03:56
  • 1
    Unrelated, but you probably should use `https` rather than `http`. The latter is not secure and you have to disable security settings to perform `http` requests. It’s better to just use `https` if your host permits. – Rob Aug 26 '21 at 03:59

0 Answers0