Sample code for 30+ languages & platforms
Xbase++

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 Xbase++ Downloads

Xbase++
LOCAL nSuccess
LOCAL oHttp
LOCAL oReq
LOCAL oResp
LOCAL oSbResponseBody
LOCAL oJResp
LOCAL nRespStatusCode
LOCAL nReturnCode
LOCAL cReturnCodeText
LOCAL cData
LOCAL oJsonData
LOCAL nMessageId
LOCAL cName
LOCAL cDisplayName
LOCAL nSize
LOCAL nWasViewed
LOCAL cLocation
LOCAL cUserName
LOCAL nAccountId
LOCAL cAccountName
LOCAL cAccountTitle
LOCAL cFirstName
LOCAL cLastName
LOCAL cLastSentOn
LOCAL nSendCount
LOCAL nIsFavorite
LOCAL nId
LOCAL nFromUserId
LOCAL nFromMailBoxId
LOCAL nFromUserType
LOCAL cFromUserName
LOCAL cFromUserFullName
LOCAL cFromUserAccountName
LOCAL cFromUserAccountTitle
LOCAL nToUserId
LOCAL nToUserType
LOCAL cToUserMailboxId
LOCAL cToUserName
LOCAL cToUserFullName
LOCAL cEmailAddress
LOCAL cPassword
LOCAL cSubject
LOCAL cPatientMedTunnelId
LOCAL cBody
LOCAL cDateReceived
LOCAL cDisplayDateReceived
LOCAL nViewCount
LOCAL cViewedOn
LOCAL nAttachmentCount
LOCAL nStatus
LOCAL nParentMessageId
LOCAL nDistributionListId
LOCAL cDistributionListName
LOCAL cBodyHistory
LOCAL cReadReceiptCallbackUrl
LOCAL nSendGlobalNotifications
LOCAL i
LOCAL nCount_i

nSuccess := 0

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

oHttp := CreateObject("Chilkat.Http")

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

oReq := CreateObject("Chilkat.HttpRequest")
oReq:HttpVerb := "POST"
oReq:Path := "/MedTunnelMsg/api/Message/SendMessage"
oReq:ContentType := "multipart/form-data"

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

nSuccess := oReq:AddFileForUpload2("file1", "qa_data/jpg/starfish.jpg", "application/octet-stream")

oReq:AddHeader("Expect", "100-continue")

oResp := CreateObject("Chilkat.HttpResponse")
nSuccess := oHttp:HttpSReq("server.medtunnel.com", 443, 1, oReq, oResp)
IF (nSuccess == 0)
    ? oHttp:LastErrorText
    oHttp:destroy()
    oReq:destroy()
    oResp:destroy()
    RETURN
ENDIF

oSbResponseBody := CreateObject("Chilkat.StringBuilder")
oResp:GetBodySb(oSbResponseBody)
oJResp := CreateObject("Chilkat.JsonObject")
oJResp:LoadSb(oSbResponseBody)
oJResp:EmitCompact := 0

? "Response Body:"
? oJResp:Emit()

nRespStatusCode := oResp:StatusCode
? "Response Status Code = " + Str(nRespStatusCode)
IF (nRespStatusCode >= 400)
    ? "Response Header:"
    ? oResp:Header
    ? "Failed."
    oHttp:destroy()
    oReq:destroy()
    oResp:destroy()
    oSbResponseBody:destroy()
    oJResp:destroy()
    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

nReturnCode := oJResp:IntOf("ReturnCode")
cReturnCodeText := oJResp:StringOf("ReturnCodeText")
cData := oJResp:StringOf("Data")

//  Load the Data into another JSON object and parse..
oJsonData := CreateObject("Chilkat.JsonObject")
oJsonData:Load(cData)
oJsonData:EmitCompact := 0
? oJsonData: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
//  }

nId := oJsonData:IntOf("Id")
nFromUserId := oJsonData:IntOf("FromUserId")
nFromMailBoxId := oJsonData:IntOf("FromMailBoxId")
nFromUserType := oJsonData:IntOf("FromUserType")
cFromUserName := oJsonData:StringOf("FromUserName")
cFromUserFullName := oJsonData:StringOf("FromUserFullName")
cFromUserAccountName := oJsonData:StringOf("FromUserAccountName")
cFromUserAccountTitle := oJsonData:StringOf("FromUserAccountTitle")
nToUserId := oJsonData:IntOf("ToUserId")
nToUserType := oJsonData:IntOf("ToUserType")
cToUserMailboxId := oJsonData:StringOf("ToUserMailboxId")
cToUserName := oJsonData:StringOf("ToUserName")
cToUserFullName := oJsonData:StringOf("ToUserFullName")
cEmailAddress := oJsonData:StringOf("EmailAddress")
cPassword := oJsonData:StringOf("Password")
cSubject := oJsonData:StringOf("Subject")
cPatientMedTunnelId := oJsonData:StringOf("PatientMedTunnelId")
cBody := oJsonData:StringOf("Body")
cDateReceived := oJsonData:StringOf("DateReceived")
cDisplayDateReceived := oJsonData:StringOf("DisplayDateReceived")
nViewCount := oJsonData:IntOf("ViewCount")
cViewedOn := oJsonData:StringOf("ViewedOn")
nAttachmentCount := oJsonData:IntOf("AttachmentCount")
nStatus := oJsonData:IntOf("Status")
nParentMessageId := oJsonData:IntOf("ParentMessageId")
nDistributionListId := oJsonData:IntOf("DistributionListId")
cDistributionListName := oJsonData:StringOf("DistributionListName")
cBodyHistory := oJsonData:StringOf("BodyHistory")
cReadReceiptCallbackUrl := oJsonData:StringOf("ReadReceiptCallbackUrl")
nSendGlobalNotifications := oJsonData:BoolOf("SendGlobalNotifications")
i := 0
nCount_i := oJsonData:SizeOfArray("AttachmentNames")
DO WHILE i < nCount_i
    oJsonData:I := i
    nMessageId := oJsonData:IntOf("AttachmentNames[i].MessageId")
    nId := oJsonData:IntOf("AttachmentNames[i].Id")
    cName := oJsonData:StringOf("AttachmentNames[i].Name")
    cDisplayName := oJsonData:StringOf("AttachmentNames[i].DisplayName")
    nSize := oJsonData:IntOf("AttachmentNames[i].Size")
    nWasViewed := oJsonData:BoolOf("AttachmentNames[i].WasViewed")
    cViewedOn := oJsonData:StringOf("AttachmentNames[i].ViewedOn")
    nViewCount := oJsonData:IntOf("AttachmentNames[i].ViewCount")
    cLocation := oJsonData:StringOf("AttachmentNames[i].Location")
    i := i + 1
ENDDO
i := 0
nCount_i := oJsonData:SizeOfArray("AllRecipients")
DO WHILE i < nCount_i
    oJsonData:I := i
    nId := oJsonData:IntOf("AllRecipients[i].Id")
    cUserName := oJsonData:StringOf("AllRecipients[i].UserName")
    nAccountId := oJsonData:IntOf("AllRecipients[i].AccountId")
    cAccountName := oJsonData:StringOf("AllRecipients[i].AccountName")
    cAccountTitle := oJsonData:StringOf("AllRecipients[i].AccountTitle")
    cFirstName := oJsonData:StringOf("AllRecipients[i].FirstName")
    cLastName := oJsonData:StringOf("AllRecipients[i].LastName")
    cEmailAddress := oJsonData:StringOf("AllRecipients[i].EmailAddress")
    cLastSentOn := oJsonData:StringOf("AllRecipients[i].LastSentOn")
    nSendCount := oJsonData:IntOf("AllRecipients[i].SendCount")
    nIsFavorite := oJsonData:BoolOf("AllRecipients[i].IsFavorite")
    i := i + 1
ENDDO

oHttp:destroy()
oReq:destroy()
oResp:destroy()
oSbResponseBody:destroy()
oJResp:destroy()
oJsonData:destroy()