Sample code for 30+ languages & platforms
Tcl

MedTunnel: Send Message with File Attachment

See more MedTunnel Examples

Demonstrates the MedTunnel SendMessage method to send a message with a file attachment to one or more recipients.

Chilkat Tcl Downloads

Tcl

load ./chilkat.dll

set success 0

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

set http [new_CkHttp]

# Implements the following CURL command:

# curl https://server.medtunnel.com/MedTunnelMsg/api/Message/SendMessage -X POST -k 
#         -F "ApplicationId=yourApplicationId" -F "LocationId=yourLocationId" 
#         -F "MedTunnelId=yourMedTunnelId" -F "MedTunnelPassword=yourMedTunnelPassword" 
#         -F "To=recipientsMedTunnelId" 
#         -F "Body=Test of SendMessage"
#         -F "file1=@qa_data/jpg/starfish.jpg"

# Use the following online tool to generate HTTP code from a CURL command
# Convert a cURL Command to HTTP Source Code

set req [new_CkHttpRequest]

CkHttpRequest_put_HttpVerb $req "POST"
CkHttpRequest_put_Path $req "/MedTunnelMsg/api/Message/SendMessage"
CkHttpRequest_put_ContentType $req "multipart/form-data"

CkHttpRequest_AddParam $req "ApplicationId" "yourApplicationId"
CkHttpRequest_AddParam $req "LocationId" "yourLocationId"
CkHttpRequest_AddParam $req "MedTunnelId" "yourMedTunnelId"
CkHttpRequest_AddParam $req "MedTunnelPassword" "yourMedTunnelPassword"
CkHttpRequest_AddParam $req "To" "recipientsMedTunnelId"
CkHttpRequest_AddParam $req "Body" "Test"

set success [CkHttpRequest_AddFileForUpload2 $req "file1" "qa_data/jpg/starfish.jpg" "application/octet-stream"]

CkHttpRequest_AddHeader $req "Expect" "100-continue"

set resp [new_CkHttpResponse]

set success [CkHttp_HttpSReq $http "server.medtunnel.com" 443 1 $req $resp]
if {$success == 0} then {
    puts [CkHttp_lastErrorText $http]
    delete_CkHttp $http
    delete_CkHttpRequest $req
    delete_CkHttpResponse $resp
    exit
}

set sbResponseBody [new_CkStringBuilder]

CkHttpResponse_GetBodySb $resp $sbResponseBody
set jResp [new_CkJsonObject]

CkJsonObject_LoadSb $jResp $sbResponseBody
CkJsonObject_put_EmitCompact $jResp 0

puts "Response Body:"
puts [CkJsonObject_emit $jResp]

set respStatusCode [CkHttpResponse_get_StatusCode $resp]
puts "Response Status Code = $respStatusCode"
if {$respStatusCode >= 400} then {
    puts "Response Header:"
    puts [CkHttpResponse_header $resp]
    puts "Failed."
    delete_CkHttp $http
    delete_CkHttpRequest $req
    delete_CkHttpResponse $resp
    delete_CkStringBuilder $sbResponseBody
    delete_CkJsonObject $jResp
    exit
}

# Sample JSON response:
# (Sample code for parsing the JSON response is shown below)

# {
#   "ReturnCode": 1,
#   "ReturnCodeText": "Success",
#   "Data": "{\"Id\":989432,\"FromUserId\":36990,\"FromMailBoxId\":36965, ... \"SendGlobalNotifications\":false}"
# }

# Sample code for parsing the JSON response...
# Use the following online tool to generate parsing code from sample JSON:
# Generate Parsing Code from JSON

set ReturnCode [CkJsonObject_IntOf $jResp "ReturnCode"]
set ReturnCodeText [CkJsonObject_stringOf $jResp "ReturnCodeText"]
set Data [CkJsonObject_stringOf $jResp "Data"]

# Load the Data into another JSON object and parse..
set jsonData [new_CkJsonObject]

CkJsonObject_Load $jsonData $Data
CkJsonObject_put_EmitCompact $jsonData 0
puts [CkJsonObject_emit $jsonData]

# {
#   "Id": 989448,
#   "FromUserId": 36990,
#   "FromMailBoxId": 36965,
#   "FromUserType": 0,
#   "FromUserName": "...",
#   "FromUserFullName": "...",
#   "FromUserAccountName": "...",
#   "FromUserAccountTitle": "...",
#   "ToUserId": 36990,
#   "ToUserType": 1,
#   "ToUserMailboxId": "36965",
#   "ToUserName": "...",
#   "ToUserFullName": "...",
#   "EmailAddress": "",
#   "Password": "",
#   "Subject": "",
#   "PatientMedTunnelId": "",
#   "Body": "Test",
#   "DateReceived": "4/29/2021 2:48:22 PM",
#   "DisplayDateReceived": "04/29/2021  2:48 PM",
#   "ViewCount": 0,
#   "ViewedOn": "",
#   "AttachmentCount": 1,
#   "AttachmentNames": [
#     {
#       "MessageId": 989448,
#       "Id": 424857,
#       "Name": "starfish.jpg.35910fe9-4118-414c-a845-4d092ca6e784",
#       "DisplayName": "starfish.jpg",
#       "Size": 6229,
#       "WasViewed": false,
#       "ViewedOn": "",
#       "ViewCount": 0,
#       "Location": "Default"
#     }
#   ],
#   "AllRecipients": [
#     {
#       "Id": 989448,
#       "UserName": "...",
#       "AccountId": 0,
#       "AccountName": "...",
#       "AccountTitle": "",
#       "FirstName": "...",
#       "LastName": "...",
#       "EmailAddress": "",
#       "LastSentOn": "",
#       "SendCount": 0,
#       "IsFavorite": false
#     }
#   ],
#   "Status": 0,
#   "ParentMessageId": 989448,
#   "DistributionListId": 0,
#   "DistributionListName": "",
#   "BodyHistory": "",
#   "ReadReceiptCallbackUrl": null,
#   "SendGlobalNotifications": false
# }

set Id [CkJsonObject_IntOf $jsonData "Id"]
set FromUserId [CkJsonObject_IntOf $jsonData "FromUserId"]
set FromMailBoxId [CkJsonObject_IntOf $jsonData "FromMailBoxId"]
set FromUserType [CkJsonObject_IntOf $jsonData "FromUserType"]
set FromUserName [CkJsonObject_stringOf $jsonData "FromUserName"]
set FromUserFullName [CkJsonObject_stringOf $jsonData "FromUserFullName"]
set FromUserAccountName [CkJsonObject_stringOf $jsonData "FromUserAccountName"]
set FromUserAccountTitle [CkJsonObject_stringOf $jsonData "FromUserAccountTitle"]
set ToUserId [CkJsonObject_IntOf $jsonData "ToUserId"]
set ToUserType [CkJsonObject_IntOf $jsonData "ToUserType"]
set ToUserMailboxId [CkJsonObject_stringOf $jsonData "ToUserMailboxId"]
set ToUserName [CkJsonObject_stringOf $jsonData "ToUserName"]
set ToUserFullName [CkJsonObject_stringOf $jsonData "ToUserFullName"]
set EmailAddress [CkJsonObject_stringOf $jsonData "EmailAddress"]
set Password [CkJsonObject_stringOf $jsonData "Password"]
set Subject [CkJsonObject_stringOf $jsonData "Subject"]
set PatientMedTunnelId [CkJsonObject_stringOf $jsonData "PatientMedTunnelId"]
set Body [CkJsonObject_stringOf $jsonData "Body"]
set DateReceived [CkJsonObject_stringOf $jsonData "DateReceived"]
set DisplayDateReceived [CkJsonObject_stringOf $jsonData "DisplayDateReceived"]
set ViewCount [CkJsonObject_IntOf $jsonData "ViewCount"]
set ViewedOn [CkJsonObject_stringOf $jsonData "ViewedOn"]
set AttachmentCount [CkJsonObject_IntOf $jsonData "AttachmentCount"]
set Status [CkJsonObject_IntOf $jsonData "Status"]
set ParentMessageId [CkJsonObject_IntOf $jsonData "ParentMessageId"]
set DistributionListId [CkJsonObject_IntOf $jsonData "DistributionListId"]
set DistributionListName [CkJsonObject_stringOf $jsonData "DistributionListName"]
set BodyHistory [CkJsonObject_stringOf $jsonData "BodyHistory"]
set ReadReceiptCallbackUrl [CkJsonObject_stringOf $jsonData "ReadReceiptCallbackUrl"]
set SendGlobalNotifications [CkJsonObject_BoolOf $jsonData "SendGlobalNotifications"]
set i 0
set count_i [CkJsonObject_SizeOfArray $jsonData "AttachmentNames"]
while {$i < $count_i} {
    CkJsonObject_put_I $jsonData $i
    set MessageId [CkJsonObject_IntOf $jsonData "AttachmentNames[i].MessageId"]
    set Id [CkJsonObject_IntOf $jsonData "AttachmentNames[i].Id"]
    set Name [CkJsonObject_stringOf $jsonData "AttachmentNames[i].Name"]
    set DisplayName [CkJsonObject_stringOf $jsonData "AttachmentNames[i].DisplayName"]
    set Size [CkJsonObject_IntOf $jsonData "AttachmentNames[i].Size"]
    set WasViewed [CkJsonObject_BoolOf $jsonData "AttachmentNames[i].WasViewed"]
    set ViewedOn [CkJsonObject_stringOf $jsonData "AttachmentNames[i].ViewedOn"]
    set ViewCount [CkJsonObject_IntOf $jsonData "AttachmentNames[i].ViewCount"]
    set Location [CkJsonObject_stringOf $jsonData "AttachmentNames[i].Location"]
    set i [expr $i + 1]
}
set i 0
set count_i [CkJsonObject_SizeOfArray $jsonData "AllRecipients"]
while {$i < $count_i} {
    CkJsonObject_put_I $jsonData $i
    set Id [CkJsonObject_IntOf $jsonData "AllRecipients[i].Id"]
    set UserName [CkJsonObject_stringOf $jsonData "AllRecipients[i].UserName"]
    set AccountId [CkJsonObject_IntOf $jsonData "AllRecipients[i].AccountId"]
    set AccountName [CkJsonObject_stringOf $jsonData "AllRecipients[i].AccountName"]
    set AccountTitle [CkJsonObject_stringOf $jsonData "AllRecipients[i].AccountTitle"]
    set FirstName [CkJsonObject_stringOf $jsonData "AllRecipients[i].FirstName"]
    set LastName [CkJsonObject_stringOf $jsonData "AllRecipients[i].LastName"]
    set EmailAddress [CkJsonObject_stringOf $jsonData "AllRecipients[i].EmailAddress"]
    set LastSentOn [CkJsonObject_stringOf $jsonData "AllRecipients[i].LastSentOn"]
    set SendCount [CkJsonObject_IntOf $jsonData "AllRecipients[i].SendCount"]
    set IsFavorite [CkJsonObject_BoolOf $jsonData "AllRecipients[i].IsFavorite"]
    set i [expr $i + 1]
}

delete_CkHttp $http
delete_CkHttpRequest $req
delete_CkHttpResponse $resp
delete_CkStringBuilder $sbResponseBody
delete_CkJsonObject $jResp
delete_CkJsonObject $jsonData