Sample code for 30+ languages & platforms
Rust

SNS Publish (Send Message)

See more Amazon SNS Examples

Sends a message to all of a topic's subscribed endpoints.

See SNS Publish for more information.

Chilkat Rust Downloads

Rust

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

let rest = chilkat::Rest::new();

// Connect to the Amazon AWS REST server.
// such as https://sns.us-west-2.amazonaws.com/
let b_tls = true;
let port = 443;
let b_auto_reconnect = true;
let _ = rest.connect("sns.us-west-2.amazonaws.com", port, b_tls, b_auto_reconnect).is_ok();

// Provide AWS credentials for the REST call.
let auth_aws = chilkat::AuthAws::new();
auth_aws.set_access_key("AWS_ACCESS_KEY");
auth_aws.set_secret_key("AWS_SECRET_KEY");
// the region should match our URL above..
auth_aws.set_region("us-west-2");
auth_aws.set_service_name("sns");

let _ = rest.set_auth_aws(&auth_aws);

let _ = rest.add_query_param("Action", "Publish");
let _ = rest.add_query_param("TopicArn", "arn:aws:sns:us-west-2:957491831129:chilkat");
// For subscribed email addresses, this is the subject of the email.
let _ = rest.add_query_param("Subject", "Test Message from Amazon SNS");
let _ = rest.add_query_param("Message", "This is a test message from Amazon SNS");

let Ok(response_xml) = rest.full_request_no_body("GET", "/") else {
    println!("{}", rest.last_error_text());
    return;
};

// A successful response will have a status code equal to 200.
if rest.response_status_code() != 200 {
    println!("response status code = {}", rest.response_status_code());
    println!("response status text = {}", rest.response_status_text());
    println!("response header: {}", rest.response_header());
    println!("response body: {}", response_xml);
    return;
}

// Examine the successful XML response.
let xml = chilkat::Xml::new();
let _ = xml.load_xml(&response_xml);
println!("{}", xml.get_xml().unwrap_or_default());

// To get the MessageId
println!("MessageId: {}", xml.chilkat_path("PublishResult|MessageId|*").unwrap_or_default());

// A sample successful response:
// <?xml version="1.0" encoding="utf-8" ?>
// <PublishResponse xmlns="http://sns.amazonaws.com/doc/2010-03-31/">
//     <PublishResult>
//         <MessageId>5e135f2e-898d-5006-940a-0f43829c764b</MessageId>
//     </PublishResult>
//     <ResponseMetadata>
//         <RequestId>9adbf47d-9e07-5981-bb7f-a4609a167fcc</RequestId>
//     </ResponseMetadata>
// </PublishResponse>