I would like to send a POST request to an API with two parameters. How to start with it?
The API is https://www.google.com/
The parameters are
1 2
id = 1234 name = wei
Answer
Below is the example for Swift 3, you can follow with that.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
var request =URLRequest(url: URL(string: "https://www.google.com/")!) request.httpMethod ="POST" let postString ="id=1234&name=wei" request.httpBody = postString.data(using: .utf8) let task =URLSession.shared.dataTask(with: request) { data, response, error in guardlet data = data, error ==nilelse { print("error=\(error)") return }
iflet httpStatus = response as?HTTPURLResponse, httpStatus.statusCode !=200 { print("statusCode should be 200, but is \(httpStatus.statusCode)") print("response = \(response)") }