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.
Asked
Active
Viewed 2,798 times
1 Answers
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
-
-
1Yes, 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