Sample code for 30+ languages & platforms
Unicode C++

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 Unicode C++ Downloads

Unicode C++
#include <CkHttpW.h>
#include <CkHttpRequestW.h>
#include <CkHttpResponseW.h>
#include <CkStringBuilderW.h>
#include <CkJsonObjectW.h>

void ChilkatSample(void)
    {
    bool success = false;

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

    CkHttpW 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

    CkHttpRequestW req;
    req.put_HttpVerb(L"POST");
    req.put_Path(L"/MedTunnelMsg/api/Message/SendMessage");
    req.put_ContentType(L"multipart/form-data");

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

    success = req.AddFileForUpload2(L"file1",L"qa_data/jpg/starfish.jpg",L"application/octet-stream");

    req.AddHeader(L"Expect",L"100-continue");

    CkHttpResponseW resp;
    success = http.HttpSReq(L"server.medtunnel.com",443,true,req,resp);
    if (success == false) {
        wprintf(L"%s\n",http.lastErrorText());
        return;
    }

    CkStringBuilderW sbResponseBody;
    resp.GetBodySb(sbResponseBody);
    CkJsonObjectW jResp;
    jResp.LoadSb(sbResponseBody);
    jResp.put_EmitCompact(false);

    wprintf(L"Response Body:\n");
    wprintf(L"%s\n",jResp.emit());

    int respStatusCode = resp.get_StatusCode();
    wprintf(L"Response Status Code = %d\n",respStatusCode);
    if (respStatusCode >= 400) {
        wprintf(L"Response Header:\n");
        wprintf(L"%s\n",resp.header());
        wprintf(L"Failed.\n");
        return;
    }

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

    //  Chilkat functions returning "const char *" return a pointer to temporary internal memory owned and managed by Chilkat.
    //  See this example explaining how this memory should be used: const char * functions.

    int ReturnCode = jResp.IntOf(L"ReturnCode");
    const wchar_t *ReturnCodeText = jResp.stringOf(L"ReturnCodeText");
    const wchar_t *Data = jResp.stringOf(L"Data");

    //  Load the Data into another JSON object and parse..
    CkJsonObjectW jsonData;
    jsonData.Load(Data);
    jsonData.put_EmitCompact(false);
    wprintf(L"%s\n",jsonData.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
    //  }

    //  Chilkat functions returning "const char *" return a pointer to temporary internal memory owned and managed by Chilkat.
    //  See this example explaining how this memory should be used: const char * functions.

    int MessageId;
    const wchar_t *Name = 0;
    const wchar_t *DisplayName = 0;
    int Size;
    bool WasViewed;
    const wchar_t *Location = 0;
    const wchar_t *UserName = 0;
    int AccountId;
    const wchar_t *AccountName = 0;
    const wchar_t *AccountTitle = 0;
    const wchar_t *FirstName = 0;
    const wchar_t *LastName = 0;
    const wchar_t *LastSentOn = 0;
    int SendCount;
    bool IsFavorite;

    int Id = jsonData.IntOf(L"Id");
    int FromUserId = jsonData.IntOf(L"FromUserId");
    int FromMailBoxId = jsonData.IntOf(L"FromMailBoxId");
    int FromUserType = jsonData.IntOf(L"FromUserType");
    const wchar_t *FromUserName = jsonData.stringOf(L"FromUserName");
    const wchar_t *FromUserFullName = jsonData.stringOf(L"FromUserFullName");
    const wchar_t *FromUserAccountName = jsonData.stringOf(L"FromUserAccountName");
    const wchar_t *FromUserAccountTitle = jsonData.stringOf(L"FromUserAccountTitle");
    int ToUserId = jsonData.IntOf(L"ToUserId");
    int ToUserType = jsonData.IntOf(L"ToUserType");
    const wchar_t *ToUserMailboxId = jsonData.stringOf(L"ToUserMailboxId");
    const wchar_t *ToUserName = jsonData.stringOf(L"ToUserName");
    const wchar_t *ToUserFullName = jsonData.stringOf(L"ToUserFullName");
    const wchar_t *EmailAddress = jsonData.stringOf(L"EmailAddress");
    const wchar_t *Password = jsonData.stringOf(L"Password");
    const wchar_t *Subject = jsonData.stringOf(L"Subject");
    const wchar_t *PatientMedTunnelId = jsonData.stringOf(L"PatientMedTunnelId");
    const wchar_t *Body = jsonData.stringOf(L"Body");
    const wchar_t *DateReceived = jsonData.stringOf(L"DateReceived");
    const wchar_t *DisplayDateReceived = jsonData.stringOf(L"DisplayDateReceived");
    int ViewCount = jsonData.IntOf(L"ViewCount");
    const wchar_t *ViewedOn = jsonData.stringOf(L"ViewedOn");
    int AttachmentCount = jsonData.IntOf(L"AttachmentCount");
    int Status = jsonData.IntOf(L"Status");
    int ParentMessageId = jsonData.IntOf(L"ParentMessageId");
    int DistributionListId = jsonData.IntOf(L"DistributionListId");
    const wchar_t *DistributionListName = jsonData.stringOf(L"DistributionListName");
    const wchar_t *BodyHistory = jsonData.stringOf(L"BodyHistory");
    const wchar_t *ReadReceiptCallbackUrl = jsonData.stringOf(L"ReadReceiptCallbackUrl");
    bool SendGlobalNotifications = jsonData.BoolOf(L"SendGlobalNotifications");
    int i = 0;
    int count_i = jsonData.SizeOfArray(L"AttachmentNames");
    while (i < count_i) {
        jsonData.put_I(i);
        MessageId = jsonData.IntOf(L"AttachmentNames[i].MessageId");
        Id = jsonData.IntOf(L"AttachmentNames[i].Id");
        Name = jsonData.stringOf(L"AttachmentNames[i].Name");
        DisplayName = jsonData.stringOf(L"AttachmentNames[i].DisplayName");
        Size = jsonData.IntOf(L"AttachmentNames[i].Size");
        WasViewed = jsonData.BoolOf(L"AttachmentNames[i].WasViewed");
        ViewedOn = jsonData.stringOf(L"AttachmentNames[i].ViewedOn");
        ViewCount = jsonData.IntOf(L"AttachmentNames[i].ViewCount");
        Location = jsonData.stringOf(L"AttachmentNames[i].Location");
        i = i + 1;
    }

    i = 0;
    count_i = jsonData.SizeOfArray(L"AllRecipients");
    while (i < count_i) {
        jsonData.put_I(i);
        Id = jsonData.IntOf(L"AllRecipients[i].Id");
        UserName = jsonData.stringOf(L"AllRecipients[i].UserName");
        AccountId = jsonData.IntOf(L"AllRecipients[i].AccountId");
        AccountName = jsonData.stringOf(L"AllRecipients[i].AccountName");
        AccountTitle = jsonData.stringOf(L"AllRecipients[i].AccountTitle");
        FirstName = jsonData.stringOf(L"AllRecipients[i].FirstName");
        LastName = jsonData.stringOf(L"AllRecipients[i].LastName");
        EmailAddress = jsonData.stringOf(L"AllRecipients[i].EmailAddress");
        LastSentOn = jsonData.stringOf(L"AllRecipients[i].LastSentOn");
        SendCount = jsonData.IntOf(L"AllRecipients[i].SendCount");
        IsFavorite = jsonData.BoolOf(L"AllRecipients[i].IsFavorite");
        i = i + 1;
    }
    }