Swift
Swift
WhatsApp - Change a User Password
Demonstrates how to change the password for a WhatsApp Business API user account.Chilkat Swift Downloads
func chilkatTest() {
var success: Bool = false
// This example assumes the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
let http = CkoHttp()!
// Implements the following CURL command:
// curl -X PUT https://example.com/v1/users/username \
// -H "Authorization: Bearer your-auth-token" \
// -d '{"password": "new-password"}'
// The following JSON is sent in the request body.
// {
// "password": "new-password"
// }
let json = CkoJsonObject()!
json.updateString(jsonPath: "password", value: "new-password")
// Adds the "Authorization: Bearer your-auth-token" header.
http.authToken = "your-auth-token"
let sbRequestBody = CkoStringBuilder()!
json.emitSb(sb: sbRequestBody)
let resp = CkoHttpResponse()!
success = http.httpSb(verb: "PUT", url: "https://example.com/v1/users/username", sb: sbRequestBody, charset: "utf-8", contentType: "application/json", response: resp)
if success == false {
print("\(http.lastErrorText!)")
return
}
let sbResponseBody = CkoStringBuilder()!
resp.getBodySb(sb: sbResponseBody)
let jResp = CkoJsonObject()!
jResp.loadSb(sb: sbResponseBody)
jResp.emitCompact = false
print("Response Body:")
print("\(jResp.emit()!)")
var respStatusCode: Int = resp.statusCode.intValue
print("Response Status Code = \(respStatusCode)")
if respStatusCode >= 400 {
print("Response Header:")
print("\(resp.header!)")
print("Failed.")
return
}
// Sample JSON response:
// (Sample code for parsing the JSON response is shown below)
// {
// "users": [
// {
// "username": "username"
// }
// ]
// }
// Sample code for parsing the JSON response...
var username: String?
var i: Int = 0
var count_i: Int = jResp.size(ofArray: "users").intValue
while i < count_i {
jResp.i = i
username = jResp.string(of: "users[i].username")
i = i + 1
}
}