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

Amazon Cognito - Admin Create User

See more Amazon Cognito Examples

Creates a new user in the specified user pool.

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.

    // {
    //     "DesiredDeliveryMediums" : [ "SMS", "EMAIL" ],	
    //     "UserAttributes": [
    //          {
    //            "Name": "phone_number",
    //            "Value": "+16302581871"
    //          },
    //          {
    //            "Name": "email",
    //            "Value": "admin@chilkatsoft.com"
    //          }
    //        ],
    //      "UserPoolId": "us-west-2_yt6WzO3SA",
    //      "Username": "Matt"
    // }
    // 

    CkJsonObjectW json;
    json.UpdateString(L"DesiredDeliveryMediums[0]",L"SMS");
    json.UpdateString(L"DesiredDeliveryMediums[1]",L"EMAIL");
    json.UpdateString(L"UserAttributes[0].Name",L"phone_number");
    json.UpdateString(L"UserAttributes[0].Value",L"+16302581871");
    json.UpdateString(L"UserAttributes[1].Name",L"email");
    json.UpdateString(L"UserAttributes[1].Value",L"admin@chilkatsoft.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.AdminCreateUser");
    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());

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

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

    // {
    //   "User": {
    //     "Attributes": [
    //       {
    //         "Name": "sub",
    //         "Value": "dcd3db0c-32cf-4a33-a6b2-173653f7c0e7"
    //       },
    //       {
    //         "Name": "phone_number",
    //         "Value": "+16302581871"
    //       },
    //       {
    //         "Name": "email",
    //         "Value": "admin@chilkatsoft.com"
    //       }
    //     ],
    //     "Enabled": true,
    //     "UserCreateDate": 1.636407153801E9,
    //     "UserLastModifiedDate": 1.636407153801E9,
    //     "UserStatus": "FORCE_CHANGE_PASSWORD",
    //     "Username": "matt"
    //   }
    // }

    const wchar_t *Name = 0;
    const wchar_t *Value = 0;

    bool UserEnabled = jsonResponse.BoolOf(L"User.Enabled");
    const wchar_t *UserUserCreateDate = jsonResponse.stringOf(L"User.UserCreateDate");
    const wchar_t *UserUserLastModifiedDate = jsonResponse.stringOf(L"User.UserLastModifiedDate");
    const wchar_t *UserUserStatus = jsonResponse.stringOf(L"User.UserStatus");
    const wchar_t *UserUsername = jsonResponse.stringOf(L"User.Username");
    int i = 0;
    int count_i = jsonResponse.SizeOfArray(L"User.Attributes");
    while (i < count_i) {
        jsonResponse.put_I(i);
        Name = jsonResponse.stringOf(L"User.Attributes[i].Name");
        Value = jsonResponse.stringOf(L"User.Attributes[i].Value");
        i = i + 1;
    }
    }