Sample code for 30+ languages & platforms
Tcl

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

Tcl

load ./chilkat.dll

set success 0

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

set rest [new_CkRest]

# Connect to the Amazon AWS REST server.
# such as https://sqs.us-west-2.amazonaws.com/
set bTls 1
set port 443
set bAutoReconnect 1
set success [CkRest_Connect $rest "sqs.us-west-2.amazonaws.com" $port $bTls $bAutoReconnect]

# Provide AWS credentials for the REST call.
set authAws [new_CkAuthAws]

CkAuthAws_put_AccessKey $authAws "AWS_ACCESS_KEY"
CkAuthAws_put_SecretKey $authAws "AWS_SECRET_KEY"
# the region should match our URL above..
CkAuthAws_put_Region $authAws "us-west-2"
CkAuthAws_put_ServiceName $authAws "sqs"

CkRest_SetAuthAws $rest $authAws

CkRest_AddQueryParam $rest "Action" "ReceiveMessage"
CkRest_AddQueryParam $rest "MaxNumberOfMessages" "5"
CkRest_AddQueryParam $rest "VisibilityTimeout" "15"
CkRest_AddQueryParam $rest "AttributeName" "All"
CkRest_AddQueryParam $rest "MessageAttributeName" "All"

# Use the actual path part of your SQS queue URL here:
set responseXml [CkRest_fullRequestNoBody $rest "GET" "/123456789123/chilkatTest"]
if {[CkRest_get_LastMethodSuccess $rest] != 1} then {
    puts [CkRest_lastErrorText $rest]
    delete_CkRest $rest
    delete_CkAuthAws $authAws
    exit
}

# A successful response will have a status code equal to 200.
if {[CkRest_get_ResponseStatusCode $rest] != 200} then {
    puts "response status code = [CkRest_get_ResponseStatusCode $rest]"
    puts "response status text = [CkRest_responseStatusText $rest]"
    puts "response header: [CkRest_responseHeader $rest]"
    puts "response body: $responseXml"
    delete_CkRest $rest
    delete_CkAuthAws $authAws
    exit
}

# Examine the successful XML response.
set xml [new_CkXml]

CkXml_LoadXml $xml $responseXml
puts [CkXml_getXml $xml]
puts "----"

# 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:
puts "Body: [CkXml_chilkatPath $xml ReceiveMessageResult|Message|Body|*]"
puts "MessageId: [CkXml_chilkatPath $xml ReceiveMessageResult|Message|MessageId|*]"

set unused [CkXml_chilkatPath $xml "ReceiveMessageResult|Message[0]|Attribute|$"]

# rec is a CkXml
set rec [CkXml_FindNextRecord $xml "Name" "SenderId"]
puts "SenderId: [CkXml_getChildContent $rec Value]"
delete_CkXml $rec

set rec [CkXml_FindNextRecord $xml "Name" "ApproximateFirstReceiveTimestamp"]
puts "ApproximateFirstReceiveTimestamp: [CkXml_getChildContent $rec Value]"
delete_CkXml $rec

set rec [CkXml_FindNextRecord $xml "Name" "ApproximateReceiveCount"]
puts "ApproximateReceiveCount: [CkXml_getChildContent $rec Value]"
delete_CkXml $rec

set rec [CkXml_FindNextRecord $xml "Name" "SentTimestamp"]
puts "SentTimestamp: [CkXml_getChildContent $rec Value]"
delete_CkXml $rec

CkXml_GetRoot2 $xml

delete_CkRest $rest
delete_CkAuthAws $authAws
delete_CkXml $xml