Sample code for 30+ languages & platforms
Lianja

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

Lianja
llSuccess = .F.

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

loHttp = createobject("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

loReq = createobject("CkHttpRequest")
loReq.HttpVerb = "POST"
loReq.Path = "/MedTunnelMsg/api/Message/SendMessage"
loReq.ContentType = "multipart/form-data"

loReq.AddParam("ApplicationId","yourApplicationId")
loReq.AddParam("LocationId","yourLocationId")
loReq.AddParam("MedTunnelId","yourMedTunnelId")
loReq.AddParam("MedTunnelPassword","yourMedTunnelPassword")
loReq.AddParam("To","recipientsMedTunnelId")
loReq.AddParam("Body","Test")

llSuccess = loReq.AddFileForUpload2("file1","qa_data/jpg/starfish.jpg","application/octet-stream")

loReq.AddHeader("Expect","100-continue")

loResp = createobject("CkHttpResponse")
llSuccess = loHttp.HttpSReq("server.medtunnel.com",443,.T.,loReq,loResp)
if (llSuccess = .F.) then
    ? loHttp.LastErrorText
    release loHttp
    release loReq
    release loResp
    return
endif

loSbResponseBody = createobject("CkStringBuilder")
loResp.GetBodySb(loSbResponseBody)
loJResp = createobject("CkJsonObject")
loJResp.LoadSb(loSbResponseBody)
loJResp.EmitCompact = .F.

? "Response Body:"
? loJResp.Emit()

lnRespStatusCode = loResp.StatusCode
? "Response Status Code = " + str(lnRespStatusCode)
if (lnRespStatusCode >= 400) then
    ? "Response Header:"
    ? loResp.Header
    ? "Failed."
    release loHttp
    release loReq
    release loResp
    release loSbResponseBody
    release loJResp
    return
endif

// 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

lnReturnCode = loJResp.IntOf("ReturnCode")
lcReturnCodeText = loJResp.StringOf("ReturnCodeText")
lcData = loJResp.StringOf("Data")

// Load the Data into another JSON object and parse..
loJsonData = createobject("CkJsonObject")
loJsonData.Load(lcData)
loJsonData.EmitCompact = .F.
? loJsonData.Emit()

// {
//   "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
// }

lnId = loJsonData.IntOf("Id")
lnFromUserId = loJsonData.IntOf("FromUserId")
lnFromMailBoxId = loJsonData.IntOf("FromMailBoxId")
lnFromUserType = loJsonData.IntOf("FromUserType")
lcFromUserName = loJsonData.StringOf("FromUserName")
lcFromUserFullName = loJsonData.StringOf("FromUserFullName")
lcFromUserAccountName = loJsonData.StringOf("FromUserAccountName")
lcFromUserAccountTitle = loJsonData.StringOf("FromUserAccountTitle")
lnToUserId = loJsonData.IntOf("ToUserId")
lnToUserType = loJsonData.IntOf("ToUserType")
lcToUserMailboxId = loJsonData.StringOf("ToUserMailboxId")
lcToUserName = loJsonData.StringOf("ToUserName")
lcToUserFullName = loJsonData.StringOf("ToUserFullName")
lcEmailAddress = loJsonData.StringOf("EmailAddress")
lcPassword = loJsonData.StringOf("Password")
lcSubject = loJsonData.StringOf("Subject")
lcPatientMedTunnelId = loJsonData.StringOf("PatientMedTunnelId")
lcBody = loJsonData.StringOf("Body")
lcDateReceived = loJsonData.StringOf("DateReceived")
lcDisplayDateReceived = loJsonData.StringOf("DisplayDateReceived")
lnViewCount = loJsonData.IntOf("ViewCount")
lcViewedOn = loJsonData.StringOf("ViewedOn")
lnAttachmentCount = loJsonData.IntOf("AttachmentCount")
lnStatus = loJsonData.IntOf("Status")
lnParentMessageId = loJsonData.IntOf("ParentMessageId")
lnDistributionListId = loJsonData.IntOf("DistributionListId")
lcDistributionListName = loJsonData.StringOf("DistributionListName")
lcBodyHistory = loJsonData.StringOf("BodyHistory")
lcReadReceiptCallbackUrl = loJsonData.StringOf("ReadReceiptCallbackUrl")
llSendGlobalNotifications = loJsonData.BoolOf("SendGlobalNotifications")
i = 0
lnCount_i = loJsonData.SizeOfArray("AttachmentNames")
do while i < lnCount_i
    loJsonData.I = i
    lnMessageId = loJsonData.IntOf("AttachmentNames[i].MessageId")
    lnId = loJsonData.IntOf("AttachmentNames[i].Id")
    lcName = loJsonData.StringOf("AttachmentNames[i].Name")
    lcDisplayName = loJsonData.StringOf("AttachmentNames[i].DisplayName")
    lnSize = loJsonData.IntOf("AttachmentNames[i].Size")
    llWasViewed = loJsonData.BoolOf("AttachmentNames[i].WasViewed")
    lcViewedOn = loJsonData.StringOf("AttachmentNames[i].ViewedOn")
    lnViewCount = loJsonData.IntOf("AttachmentNames[i].ViewCount")
    lcLocation = loJsonData.StringOf("AttachmentNames[i].Location")
    i = i + 1
enddo
i = 0
lnCount_i = loJsonData.SizeOfArray("AllRecipients")
do while i < lnCount_i
    loJsonData.I = i
    lnId = loJsonData.IntOf("AllRecipients[i].Id")
    lcUserName = loJsonData.StringOf("AllRecipients[i].UserName")
    lnAccountId = loJsonData.IntOf("AllRecipients[i].AccountId")
    lcAccountName = loJsonData.StringOf("AllRecipients[i].AccountName")
    lcAccountTitle = loJsonData.StringOf("AllRecipients[i].AccountTitle")
    lcFirstName = loJsonData.StringOf("AllRecipients[i].FirstName")
    lcLastName = loJsonData.StringOf("AllRecipients[i].LastName")
    lcEmailAddress = loJsonData.StringOf("AllRecipients[i].EmailAddress")
    lcLastSentOn = loJsonData.StringOf("AllRecipients[i].LastSentOn")
    lnSendCount = loJsonData.IntOf("AllRecipients[i].SendCount")
    llIsFavorite = loJsonData.BoolOf("AllRecipients[i].IsFavorite")
    i = i + 1
enddo


release loHttp
release loReq
release loResp
release loSbResponseBody
release loJResp
release loJsonData