Sample code for 30+ languages & platforms
Rust

SQS Receive Message

See more Amazon SQS Examples

Retrieves one or more messages from an Amazon SQS queue, with a maximum limit of 10 messages, from the specified queue.

See SQS ReceiveMessage or detailed 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://sqs.us-west-2.amazonaws.com/
let b_tls = true;
let port = 443;
let b_auto_reconnect = true;
let _ = rest.connect("sqs.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("sqs");

let _ = rest.set_auth_aws(&auth_aws);

let _ = rest.add_query_param("Action", "ReceiveMessage");
let _ = rest.add_query_param("MaxNumberOfMessages", "5");
let _ = rest.add_query_param("VisibilityTimeout", "15");
let _ = rest.add_query_param("AttributeName", "All");
let _ = rest.add_query_param("MessageAttributeName", "All");

// Use the actual path part of your SQS queue URL here:
let Ok(response_xml) = rest.full_request_no_body("GET", "/123456789123/chilkatTest") 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());
println!("----");

// A successful response looks like this:
// <?xml version="1.0" encoding="utf-8" ?>
// <ReceiveMessageResponse xmlns="http://queue.amazonaws.com/doc/2012-11-05/">
//     <ReceiveMessageResult>
//         <Message>
//             <Body>this is a test</Body>
//             <MD5OfBody>54b0c58c7ce9f2a8b551351102ee0938</MD5OfBody>
//             <ReceiptHandle>AQEBMyLof...UbKBTvHtEg==</ReceiptHandle>
//             <Attribute>
//                 <Name>SenderId</Name>
//                 <Value>957491831129</Value>
//             </Attribute>
//             <Attribute>
//                 <Name>ApproximateFirstReceiveTimestamp</Name>
//                 <Value>1475013283557</Value>
//             </Attribute>
//             <Attribute>
//                 <Name>ApproximateReceiveCount</Name>
//                 <Value>4</Value>
//             </Attribute>
//             <Attribute>
//                 <Name>SentTimestamp</Name>
//                 <Value>1475013283557</Value>
//             </Attribute>
//             <MessageId>52882c65-5e21-4450-9024-93a3b01e61d3</MessageId>
//             <MD5OfMessageAttributes>2ff68603f4239272bd03f543254ed040</MD5OfMessageAttributes>
//         </Message>
//     </ReceiveMessageResult>
//     <ResponseMetadata>
//         <RequestId>05ba5c92-80ee-5f87-be76-0cf67b7475d6</RequestId>
//     </ResponseMetadata>
// </ReceiveMessageResponse>

// Get some values from the XML:
println!("Body: {}", xml.chilkat_path("ReceiveMessageResult|Message|Body|*").unwrap_or_default());
println!("MessageId: {}", xml.chilkat_path("ReceiveMessageResult|Message|MessageId|*").unwrap_or_default());

let unused = xml.chilkat_path("ReceiveMessageResult|Message[0]|Attribute|$").unwrap_or_default();

let mut rec = xml.find_next_record("Name", "SenderId").unwrap();
println!("SenderId: {}", rec.get_child_content("Value").unwrap_or_default());

rec = xml.find_next_record("Name", "ApproximateFirstReceiveTimestamp").unwrap();
println!("ApproximateFirstReceiveTimestamp: {}", rec.get_child_content("Value").unwrap_or_default());

rec = xml.find_next_record("Name", "ApproximateReceiveCount").unwrap();
println!("ApproximateReceiveCount: {}", rec.get_child_content("Value").unwrap_or_default());

rec = xml.find_next_record("Name", "SentTimestamp").unwrap();
println!("SentTimestamp: {}", rec.get_child_content("Value").unwrap_or_default());

xml.get_root2();