Swift
Swift
Send a Form-URL-Encoded REST Request
See more REST Examples
Demonstrates Rest.FullRequestFormUrlEncoded, which sends an application/x-www-form-urlencoded request. The body is built from the query parameters added to the object, and the response body is returned as text.
Background. For a form-url-encoded request the accumulated parameters become the request body rather than the URL query string. This matches the encoding used by HTML form POSTs.
Chilkat Swift Downloads
func chilkatTest() {
var success: Bool = false
let rest = CkoRest()!
var bTls: Bool = true
var bAutoReconnect: Bool = true
success = rest.connect(hostname: "example.com", port: 443, tls: bTls, autoReconnect: bAutoReconnect)
if success == false {
print("\(rest.lastErrorText!)")
return
}
// The application/x-www-form-urlencoded body is built from the query parameters added to the object.
rest.addQueryParam(name: "username", value: "alice")
rest.addQueryParam(name: "action", value: "login")
// Send the form-url-encoded request. The parameters become the request body rather than the URL
// query string.
var responseText: String? = rest.fullRequestFormUrlEncoded(httpVerb: "POST", uriPath: "/api/login")
if rest.lastMethodSuccess == false {
print("\(rest.lastErrorText!)")
return
}
print("\(responseText!)")
}