Sample code for 30+ languages & platforms
Unicode C++

WhatsApp - Change a User Password

Demonstrates how to change the password for a WhatsApp Business API user account.

Chilkat Unicode C++ Downloads

Unicode C++
#include <CkHttpW.h>
#include <CkJsonObjectW.h>
#include <CkStringBuilderW.h>
#include <CkHttpResponseW.h>

void ChilkatSample(void)
    {
    bool success = false;

    //  This example assumes the Chilkat API to have been previously unlocked.
    //  See Global Unlock Sample for sample code.

    CkHttpW http;

    //  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"
    //  }

    CkJsonObjectW json;
    json.UpdateString(L"password",L"new-password");

    //  Adds the "Authorization: Bearer your-auth-token" header.
    http.put_AuthToken(L"your-auth-token");

    CkStringBuilderW sbRequestBody;
    json.EmitSb(sbRequestBody);

    CkHttpResponseW resp;
    success = http.HttpSb(L"PUT",L"https://example.com/v1/users/username",sbRequestBody,L"utf-8",L"application/json",resp);
    if (success == false) {
        wprintf(L"%s\n",http.lastErrorText());
        return;
    }

    CkStringBuilderW sbResponseBody;
    resp.GetBodySb(sbResponseBody);
    CkJsonObjectW jResp;
    jResp.LoadSb(sbResponseBody);
    jResp.put_EmitCompact(false);

    wprintf(L"Response Body:\n");
    wprintf(L"%s\n",jResp.emit());

    int respStatusCode = resp.get_StatusCode();
    wprintf(L"Response Status Code = %d\n",respStatusCode);
    if (respStatusCode >= 400) {
        wprintf(L"Response Header:\n");
        wprintf(L"%s\n",resp.header());
        wprintf(L"Failed.\n");
        return;
    }

    //  Sample JSON response:
    //  (Sample code for parsing the JSON response is shown below)

    //  {
    //    "users": [
    //      {
    //        "username": "username"
    //      }
    //    ]
    //  }

    //  Sample code for parsing the JSON response...

    //  Chilkat functions returning "const char *" return a pointer to temporary internal memory owned and managed by Chilkat.
    //  See this example explaining how this memory should be used: const char * functions.

    const wchar_t *username = 0;

    int i = 0;
    int count_i = jResp.SizeOfArray(L"users");
    while (i < count_i) {
        jResp.put_I(i);
        username = jResp.stringOf(L"users[i].username");
        i = i + 1;
    }
    }