Swift
Swift
WhatsApp - Get a User's Account Information
Demonstrates how to retrieve a user account by sending a GET request on the users endpoint.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 GET https://example.com/v1/users/username \
// -H "Authorization: Bearer your-auth-token"
// Adds the "Authorization: Bearer your-auth-token" header.
http.authToken = "your-auth-token"
let sbResponseBody = CkoStringBuilder()!
success = http.quickGetSb(url: "https://example.com/v1/users/username", sbContent: sbResponseBody)
if success == false {
print("\(http.lastErrorText!)")
return
}
let jResp = CkoJsonObject()!
jResp.loadSb(sb: sbResponseBody)
jResp.emitCompact = false
print("Response Body:")
print("\(jResp.emit()!)")
var respStatusCode: Int = http.lastStatus.intValue
print("Response Status Code = \(respStatusCode)")
if respStatusCode >= 400 {
print("Response Header:")
print("\(http.lastHeader!)")
print("Failed.")
return
}
// Sample JSON response:
// (Sample code for parsing the JSON response is shown below)
// {
// "users": [
// {
// "ROLES": "ROLE_ADMIN | ROLE_USER",
// "username": "username"
// }
// ]
// }
// Sample code for parsing the JSON response...
var ROLES: String?
var username: String?
var i: Int = 0
var count_i: Int = jResp.size(ofArray: "users").intValue
while i < count_i {
jResp.i = i
ROLES = jResp.string(of: "users[i].ROLES")
username = jResp.string(of: "users[i].username")
i = i + 1
}
}