Rust
Rust
Amazon Cognito - Admin Create User
See more Amazon Cognito Examples
Creates a new user in the specified user pool.Chilkat Rust Downloads
// This example requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
let rest = chilkat::Rest::new();
let auth_aws = chilkat::AuthAws::new();
auth_aws.set_access_key("AWS_ACCESS_KEY");
auth_aws.set_secret_key("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.)
auth_aws.set_region("us-west-2");
auth_aws.set_service_name("cognito-idp");
// SetAuthAws causes Chilkat to automatically add the following headers: Authorization, X-Amz-Date
let _ = rest.set_auth_aws(&auth_aws);
// URL: https://cognito-idp.us-west-2.amazonaws.com/
let b_tls = true;
let port = 443;
let b_auto_reconnect = true;
// Use the same region as specified above.
if rest.connect("cognito-idp.us-west-2.amazonaws.com", port, b_tls, b_auto_reconnect).is_err() {
println!("ConnectFailReason: {}", rest.connect_fail_reason());
println!("{}", rest.last_error_text());
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"
// }
//
let json = chilkat::JsonObject::new();
let _ = json.update_string("DesiredDeliveryMediums[0]", "SMS");
let _ = json.update_string("DesiredDeliveryMediums[1]", "EMAIL");
let _ = json.update_string("UserAttributes[0].Name", "phone_number");
let _ = json.update_string("UserAttributes[0].Value", "+16302581871");
let _ = json.update_string("UserAttributes[1].Name", "email");
let _ = json.update_string("UserAttributes[1].Value", "admin@chilkatsoft.com");
let _ = json.update_string("UserPoolId", "us-west-2_yt6WzO3SA");
let _ = json.update_string("Username", "Matt");
let _ = rest.add_header("Content-Type", "application/x-amz-json-1.0");
let _ = rest.add_header("X-Amz-Target", "AWSCognitoIdentityProviderService.AdminCreateUser");
let _ = rest.add_header("Accept-Encoding", "identity");
let sb_request_body = chilkat::StringBuilder::new();
let _ = json.emit_sb(&sb_request_body);
let sb_response_body = chilkat::StringBuilder::new();
if rest.full_request_sb("POST", "/", &sb_request_body, &sb_response_body).is_err() {
println!("{}", rest.last_error_text());
return;
}
let resp_status_code = rest.response_status_code();
println!("response status code = {}", resp_status_code);
if resp_status_code != 200 {
println!("Response Status Code = {}", resp_status_code);
println!("Response Header:");
println!("{}", rest.response_header());
println!("Response Body:");
println!("{}", sb_response_body.get_as_string().unwrap_or_default());
return;
}
let json_response = chilkat::JsonObject::new();
let _ = json_response.load_sb(&sb_response_body);
json_response.set_emit_compact(false);
println!("{}", json_response.emit().unwrap_or_default());
// 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"
// }
// }
let user_enabled = json_response.bool_of("User.Enabled");
let user_user_create_date = json_response.string_of("User.UserCreateDate").unwrap_or_default();
let user_user_last_modified_date = json_response.string_of("User.UserLastModifiedDate").unwrap_or_default();
let user_user_status = json_response.string_of("User.UserStatus").unwrap_or_default();
let user_username = json_response.string_of("User.Username").unwrap_or_default();
let mut i = 0;
let count_i = json_response.size_of_array("User.Attributes");
while i < count_i {
json_response.set_i(i);
let _ = json_response.string_of("User.Attributes[i].Name").unwrap_or_default();
let _ = json_response.string_of("User.Attributes[i].Value").unwrap_or_default();
i = i + 1;
}