-3

I'm fairly new to Swift. I am trying to make an HTTPS POST to https url with a specific header. Note HTTPS not HTTP. How do I accomplish this? Thanks in advance.

Felix
  • 51
  • 2
  • 10

1 Answers1

1

Use setValue(_:forHTTPHeaderField).

For example, to set the Content-Type of application/x-www-form-urlencoded:

var request = URLRequest(url: url)
request.setValue("application/x-www-form-urlencoded", forHTTPHeaderField: "Content-Type")
request.httpMethod = "POST"
request.httpBody = "id=42".data(using: .utf8)

let task = URLSession.shared.dataTask(with: request) { data, response, error in
    // ...
}
task.resume()
Rob
  • 415,655
  • 72
  • 787
  • 1,044
  • Will this work with an HTTPS url? – Felix Oct 13 '17 at 22:22
  • 1
    Yes, it will. In fact, it's easier because you don't have to do anything silly with the "App Transport Security Settings" in the Info.plist with https. – Rob Oct 13 '17 at 22:24