Sample code for 30+ languages & platforms
Zig

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 Zig Downloads

Zig
const std = @import("std");
const chilkat = @import("chilkat");

pub fn main(init: std.process.Init) !void {
    const alloc = init.arena.allocator();

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

    const rest = try chilkat.Rest.init();
    defer rest.deinit();

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

    // Provide AWS credentials for the REST call.
    const auth_aws = try chilkat.AuthAws.init();
    defer auth_aws.deinit();
    auth_aws.setAccessKey("AWS_ACCESS_KEY");
    auth_aws.setSecretKey("AWS_SECRET_KEY");
    // the region should match our URL above..
    auth_aws.setRegion("us-west-2");
    auth_aws.setServiceName("sqs");

    rest.setAuthAws(auth_aws) catch {};

    rest.addQueryParam("Action", "ReceiveMessage") catch {};
    rest.addQueryParam("MaxNumberOfMessages", "5") catch {};
    rest.addQueryParam("VisibilityTimeout", "15") catch {};
    rest.addQueryParam("AttributeName", "All") catch {};
    rest.addQueryParam("MessageAttributeName", "All") catch {};

    // Use the actual path part of your SQS queue URL here:
    const response_xml = rest.fullRequestNoBody(alloc, "GET", "/123456789123/chilkatTest") catch {
        std.debug.print("{s}\n", .{try rest.getLastErrorText(alloc)});
        return;
    };

    // A successful response will have a status code equal to 200.
    if (rest.getResponseStatusCode() != 200) {
        std.debug.print("response status code = {d}\n", .{rest.getResponseStatusCode()});
        std.debug.print("response status text = {s}\n", .{try rest.getResponseStatusText(alloc)});
        std.debug.print("response header: {s}\n", .{try rest.getResponseHeader(alloc)});
        std.debug.print("response body: {s}\n", .{response_xml});
        return;
    }

    // Examine the successful XML response.
    const xml = try chilkat.Xml.init();
    defer xml.deinit();
    xml.loadXml(response_xml) catch {};
    std.debug.print("{s}\n", .{try xml.getXml(alloc)});
    std.debug.print("----\n", .{});

    // 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:
    std.debug.print("Body: {s}\n", .{try xml.chilkatPath(alloc, "ReceiveMessageResult|Message|Body|*")});
    std.debug.print("MessageId: {s}\n", .{try xml.chilkatPath(alloc, "ReceiveMessageResult|Message|MessageId|*")});

    _ = try xml.chilkatPath(alloc, "ReceiveMessageResult|Message[0]|Attribute|$");

    var rec: chilkat.Xml = try xml.findNextRecord("Name", "SenderId");
    defer rec.deinit();
    std.debug.print("SenderId: {s}\n", .{try rec.getChildContent(alloc, "Value")});

    {
        const next = try xml.findNextRecord("Name", "ApproximateFirstReceiveTimestamp");
        rec.deinit();
        rec = next;
    }
    std.debug.print("ApproximateFirstReceiveTimestamp: {s}\n", .{try rec.getChildContent(alloc, "Value")});

    {
        const next = try xml.findNextRecord("Name", "ApproximateReceiveCount");
        rec.deinit();
        rec = next;
    }
    std.debug.print("ApproximateReceiveCount: {s}\n", .{try rec.getChildContent(alloc, "Value")});

    {
        const next = try xml.findNextRecord("Name", "SentTimestamp");
        rec.deinit();
        rec = next;
    }
    std.debug.print("SentTimestamp: {s}\n", .{try rec.getChildContent(alloc, "Value")});

    xml.getRoot2();
}