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

Amazon Cognito - Admin Update User Attributes

See more Amazon Cognito Examples

Updates the specified user's attributes, including developer attributes, as an administrator. Works on any user.

For custom attributes, you must prepend the custom: prefix to the attribute name.

In addition to updating user attributes, this API can also be used to mark phone and email as verified.

Chilkat Unicode C++ Downloads

Unicode C++
#include <CkRestW.h>
#include <CkAuthAwsW.h>
#include <CkJsonObjectW.h>
#include <CkStringBuilderW.h>

void ChilkatSample(void)
    {
    bool success = false;

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

    CkRestW rest;

    CkAuthAwsW authAws;
    authAws.put_AccessKey(L"AWS_ACCESS_KEY");
    authAws.put_SecretKey(L"AWS_SECRET_KEY");
    // Don't forget to change the region to your particular region. (Also make the same change in the call to Connect below.)
    authAws.put_Region(L"us-west-2");
    authAws.put_ServiceName(L"cognito-idp");
    // SetAuthAws causes Chilkat to automatically add the following headers: Authorization, X-Amz-Date
    rest.SetAuthAws(authAws);

    // URL: https://cognito-idp.us-west-2.amazonaws.com/
    bool bTls = true;
    int port = 443;
    bool bAutoReconnect = true;
    // Use the same region as specified above.
    success = rest.Connect(L"cognito-idp.us-west-2.amazonaws.com",port,bTls,bAutoReconnect);
    if (success != true) {
        wprintf(L"ConnectFailReason: %d\n",rest.get_ConnectFailReason());
        wprintf(L"%s\n",rest.lastErrorText());
        return;
    }

    // Use this online tool to generate code from sample JSON:
    // Generate Code to Create JSON

    // The following JSON is sent in the request body.

    // {
    //     "UserAttributes": [
    //          {
    //            "Name": "email_verified",
    //            "Value": "true"
    //          },
    //          {
    //            "Name": "email",
    //            "Value": "matt@example.com"
    //          }
    //        ],
    //      "UserPoolId": "us-west-2_yt6WzO3SA",
    //      "Username": "Matt"
    // }
    // 

    CkJsonObjectW json;
    json.UpdateString(L"UserAttributes[0].Name",L"email_verified");
    json.UpdateString(L"UserAttributes[0].Value",L"true");
    json.UpdateString(L"UserAttributes[1].Name",L"email");
    json.UpdateString(L"UserAttributes[1].Value",L"matt@example.com");
    json.UpdateString(L"UserPoolId",L"us-west-2_yt6WzO3SA");
    json.UpdateString(L"Username",L"Matt");

    rest.AddHeader(L"Content-Type",L"application/x-amz-json-1.0");
    rest.AddHeader(L"X-Amz-Target",L"AWSCognitoIdentityProviderService.AdminUpdateUserAttributes");
    rest.AddHeader(L"Accept-Encoding",L"identity");

    CkStringBuilderW sbRequestBody;
    json.EmitSb(sbRequestBody);
    CkStringBuilderW sbResponseBody;
    success = rest.FullRequestSb(L"POST",L"/",sbRequestBody,sbResponseBody);
    if (success != true) {
        wprintf(L"%s\n",rest.lastErrorText());
        return;
    }

    int respStatusCode = rest.get_ResponseStatusCode();
    wprintf(L"response status code = %d\n",respStatusCode);
    if (respStatusCode != 200) {
        wprintf(L"Response Status Code = %d\n",respStatusCode);
        wprintf(L"Response Header:\n");
        wprintf(L"%s\n",rest.responseHeader());
        wprintf(L"Response Body:\n");
        wprintf(L"%s\n",sbResponseBody.getAsString());
        return;
    }

    CkJsonObjectW jsonResponse;
    jsonResponse.LoadSb(sbResponseBody);

    jsonResponse.put_EmitCompact(false);
    wprintf(L"%s\n",jsonResponse.emit());

    // If successful, the response status code is 200, and the response body is empty JSON "{}"
    }