Sample code for 30+ languages & platforms
Zig

SQS Send Message

See more Amazon SQS Examples

Note: This example requires a patch to work properly. It requires Chilkat v9.5.0.61, which can be obtained upon request to support@chilkatsoft.com

Delivers a message to the specified queue. With Amazon SQS, you now have the ability to send large payload messages that are up to 256KB (262,144 bytes) in size.

See SQS SendMessage 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();

    // Note: This example requires a patch to work properly.
    // It requires Chilkat v9.5.0.61, which can be obtained upon request to support@chilkatsoft.com

    // 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", "SendMessage") catch {};
    rest.addQueryParam("MessageBody", "This is the message body which can be up to 256K in size.") catch {};
    rest.addQueryParam("MessageAttribute.1.Name", "test_attribute_name_1") catch {};
    rest.addQueryParam("MessageAttribute.1.Value.StringValue", "test_attribute_value_1") catch {};
    rest.addQueryParam("MessageAttribute.1.Value.DataType", "String") catch {};
    rest.addQueryParam("MessageAttribute.2.Name", "test_attribute_name_2") catch {};
    rest.addQueryParam("MessageAttribute.2.Value.StringValue", "test_attribute_value_2") catch {};
    rest.addQueryParam("MessageAttribute.2.Value.DataType", "String") catch {};

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

    const xml = try chilkat.Xml.init();
    defer xml.deinit();

    // A successful response will have a status code equal to 200.
    if (rest.getResponseStatusCode() != 200) {
        std.debug.print("request header = {s}\n", .{try rest.getLastRequestHeader(alloc)});
        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)});
        xml.loadXml(response_xml) catch {};
        std.debug.print("{s}\n", .{try xml.getXml(alloc)});
        return;
    }

    // Examine the successful XML response.
    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" ?>
    // <SendMessageResponse>
    //     <SendMessageResult>
    //         <MD5OfMessageBody>
    //             fafb00f5732ab283681e124bf8747ed1
    //         </MD5OfMessageBody>
    //         <MD5OfMessageAttributes>
    //         3ae8f24a165a8cedc005670c81a27295
    //         </MD5OfMessageAttributes>
    //         <MessageId>
    //             5fea7756-0ea4-451a-a703-a558b933e274
    //         </MessageId>
    //     </SendMessageResult>
    //     <ResponseMetadata>
    //         <RequestId>
    //             27daac76-34dd-47df-bd01-1f6e873584a0
    //         </RequestId>
    //     </ResponseMetadata>
    // </SendMessageResponse>

    // Get some values from the response XML:
    std.debug.print("Body: {s}\n", .{try xml.chilkatPath(alloc, "SendMessageResult|MD5OfMessageBody|*")});
    std.debug.print("MessageId: {s}\n", .{try xml.chilkatPath(alloc, "SendMessageResult|MessageId|*")});
    std.debug.print("RequestId: {s}\n", .{try xml.chilkatPath(alloc, "ResponseMetadata|RequestId|*")});
}