Swift
Swift
POST application/x-www-form-urlencoded using REST API
See more REST Examples
Demonstrates how to send a POST with query params (x-www-form-urlencoded) using the Chilkat REST object.Chilkat Swift Downloads
func chilkatTest() {
var success: Bool = false
// This example requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
let rest = CkoRest()!
// This example will send to https://www.chilkatsoft.com/echoPost.asp
// Make the initial connection (without sending a request yet).
var bTls: Bool = true
var port: Int = 443
var bAutoReconnect: Bool = true
success = rest.connect(hostname: "www.chilkatsoft.com", port: port, tls: bTls, autoReconnect: bAutoReconnect)
if success != true {
print("\(rest.lastErrorText!)")
return
}
// Provide query params.
rest.addQueryParam(name: "firstName", value: "John")
rest.addQueryParam(name: "lastName", value: "Doe")
rest.addQueryParam(name: "company", value: "Bisco Bits Ltd.")
var responseStr: String? = rest.fullRequestFormUrlEncoded(httpVerb: "POST", uriPath: "/echoPost.asp")
if rest.lastMethodSuccess != true {
print("\(rest.lastErrorText!)")
return
}
// When successful, the response status code will equal 200.
if rest.responseStatusCode.intValue != 200 {
// Examine the request/response to see what happened.
print("response status code = \(rest.responseStatusCode.intValue)")
print("response status text = \(rest.responseStatusText!)")
print("response header: \(rest.responseHeader!)")
print("response body (if any): \(responseStr!)")
print("---")
print("LastRequestStartLine: \(rest.lastRequestStartLine!)")
print("LastRequestHeader: \(rest.lastRequestHeader!)")
return
}
print("\(responseStr!)")
print("Success.")
}