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 <C_CkHttpW.h>
#include <C_CkHttpRequestW.h>
#include <C_CkHttpResponseW.h>
#include <C_CkStringBuilderW.h>
#include <C_CkJsonObjectW.h>

void ChilkatSample(void)
    {
    BOOL success;
    HCkHttpW http;
    HCkHttpRequestW req;
    HCkHttpResponseW resp;
    HCkStringBuilderW sbResponseBody;
    HCkJsonObjectW jResp;
    int respStatusCode;
    int ReturnCode;
    const wchar_t *ReturnCodeText;
    const wchar_t *Data;
    HCkJsonObjectW jsonData;
    int MessageId;
    const wchar_t *Name;
    const wchar_t *DisplayName;
    int Size;
    BOOL WasViewed;
    const wchar_t *Location;
    const wchar_t *UserName;
    int AccountId;
    const wchar_t *AccountName;
    const wchar_t *AccountTitle;
    const wchar_t *FirstName;
    const wchar_t *LastName;
    const wchar_t *LastSentOn;
    int SendCount;
    BOOL IsFavorite;
    int Id;
    int FromUserId;
    int FromMailBoxId;
    int FromUserType;
    const wchar_t *FromUserName;
    const wchar_t *FromUserFullName;
    const wchar_t *FromUserAccountName;
    const wchar_t *FromUserAccountTitle;
    int ToUserId;
    int ToUserType;
    const wchar_t *ToUserMailboxId;
    const wchar_t *ToUserName;
    const wchar_t *ToUserFullName;
    const wchar_t *EmailAddress;
    const wchar_t *Password;
    const wchar_t *Subject;
    const wchar_t *PatientMedTunnelId;
    const wchar_t *Body;
    const wchar_t *DateReceived;
    const wchar_t *DisplayDateReceived;
    int ViewCount;
    const wchar_t *ViewedOn;
    int AttachmentCount;
    int Status;
    int ParentMessageId;
    int DistributionListId;
    const wchar_t *DistributionListName;
    const wchar_t *BodyHistory;
    const wchar_t *ReadReceiptCallbackUrl;
    BOOL SendGlobalNotifications;
    int i;
    int count_i;

    success = FALSE;

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

    http = CkHttpW_Create();

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

    req = CkHttpRequestW_Create();
    CkHttpRequestW_putHttpVerb(req,L"POST");
    CkHttpRequestW_putPath(req,L"/MedTunnelMsg/api/Message/SendMessage");
    CkHttpRequestW_putContentType(req,L"multipart/form-data");

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

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

    CkHttpRequestW_AddHeader(req,L"Expect",L"100-continue");

    resp = CkHttpResponseW_Create();
    success = CkHttpW_HttpSReq(http,L"server.medtunnel.com",443,TRUE,req,resp);
    if (success == FALSE) {
        wprintf(L"%s\n",CkHttpW_lastErrorText(http));
        CkHttpW_Dispose(http);
        CkHttpRequestW_Dispose(req);
        CkHttpResponseW_Dispose(resp);
        return;
    }

    sbResponseBody = CkStringBuilderW_Create();
    CkHttpResponseW_GetBodySb(resp,sbResponseBody);
    jResp = CkJsonObjectW_Create();
    CkJsonObjectW_LoadSb(jResp,sbResponseBody);
    CkJsonObjectW_putEmitCompact(jResp,FALSE);

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

    respStatusCode = CkHttpResponseW_getStatusCode(resp);
    wprintf(L"Response Status Code = %d\n",respStatusCode);
    if (respStatusCode >= 400) {
        wprintf(L"Response Header:\n");
        wprintf(L"%s\n",CkHttpResponseW_header(resp));
        wprintf(L"Failed.\n");
        CkHttpW_Dispose(http);
        CkHttpRequestW_Dispose(req);
        CkHttpResponseW_Dispose(resp);
        CkStringBuilderW_Dispose(sbResponseBody);
        CkJsonObjectW_Dispose(jResp);
        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.

    ReturnCode = CkJsonObjectW_IntOf(jResp,L"ReturnCode");
    ReturnCodeText = CkJsonObjectW_stringOf(jResp,L"ReturnCodeText");
    Data = CkJsonObjectW_stringOf(jResp,L"Data");

    // Load the Data into another JSON object and parse..
    jsonData = CkJsonObjectW_Create();
    CkJsonObjectW_Load(jsonData,Data);
    CkJsonObjectW_putEmitCompact(jsonData,FALSE);
    wprintf(L"%s\n",CkJsonObjectW_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
    // }

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

    Id = CkJsonObjectW_IntOf(jsonData,L"Id");
    FromUserId = CkJsonObjectW_IntOf(jsonData,L"FromUserId");
    FromMailBoxId = CkJsonObjectW_IntOf(jsonData,L"FromMailBoxId");
    FromUserType = CkJsonObjectW_IntOf(jsonData,L"FromUserType");
    FromUserName = CkJsonObjectW_stringOf(jsonData,L"FromUserName");
    FromUserFullName = CkJsonObjectW_stringOf(jsonData,L"FromUserFullName");
    FromUserAccountName = CkJsonObjectW_stringOf(jsonData,L"FromUserAccountName");
    FromUserAccountTitle = CkJsonObjectW_stringOf(jsonData,L"FromUserAccountTitle");
    ToUserId = CkJsonObjectW_IntOf(jsonData,L"ToUserId");
    ToUserType = CkJsonObjectW_IntOf(jsonData,L"ToUserType");
    ToUserMailboxId = CkJsonObjectW_stringOf(jsonData,L"ToUserMailboxId");
    ToUserName = CkJsonObjectW_stringOf(jsonData,L"ToUserName");
    ToUserFullName = CkJsonObjectW_stringOf(jsonData,L"ToUserFullName");
    EmailAddress = CkJsonObjectW_stringOf(jsonData,L"EmailAddress");
    Password = CkJsonObjectW_stringOf(jsonData,L"Password");
    Subject = CkJsonObjectW_stringOf(jsonData,L"Subject");
    PatientMedTunnelId = CkJsonObjectW_stringOf(jsonData,L"PatientMedTunnelId");
    Body = CkJsonObjectW_stringOf(jsonData,L"Body");
    DateReceived = CkJsonObjectW_stringOf(jsonData,L"DateReceived");
    DisplayDateReceived = CkJsonObjectW_stringOf(jsonData,L"DisplayDateReceived");
    ViewCount = CkJsonObjectW_IntOf(jsonData,L"ViewCount");
    ViewedOn = CkJsonObjectW_stringOf(jsonData,L"ViewedOn");
    AttachmentCount = CkJsonObjectW_IntOf(jsonData,L"AttachmentCount");
    Status = CkJsonObjectW_IntOf(jsonData,L"Status");
    ParentMessageId = CkJsonObjectW_IntOf(jsonData,L"ParentMessageId");
    DistributionListId = CkJsonObjectW_IntOf(jsonData,L"DistributionListId");
    DistributionListName = CkJsonObjectW_stringOf(jsonData,L"DistributionListName");
    BodyHistory = CkJsonObjectW_stringOf(jsonData,L"BodyHistory");
    ReadReceiptCallbackUrl = CkJsonObjectW_stringOf(jsonData,L"ReadReceiptCallbackUrl");
    SendGlobalNotifications = CkJsonObjectW_BoolOf(jsonData,L"SendGlobalNotifications");
    i = 0;
    count_i = CkJsonObjectW_SizeOfArray(jsonData,L"AttachmentNames");
    while (i < count_i) {
        CkJsonObjectW_putI(jsonData,i);
        MessageId = CkJsonObjectW_IntOf(jsonData,L"AttachmentNames[i].MessageId");
        Id = CkJsonObjectW_IntOf(jsonData,L"AttachmentNames[i].Id");
        Name = CkJsonObjectW_stringOf(jsonData,L"AttachmentNames[i].Name");
        DisplayName = CkJsonObjectW_stringOf(jsonData,L"AttachmentNames[i].DisplayName");
        Size = CkJsonObjectW_IntOf(jsonData,L"AttachmentNames[i].Size");
        WasViewed = CkJsonObjectW_BoolOf(jsonData,L"AttachmentNames[i].WasViewed");
        ViewedOn = CkJsonObjectW_stringOf(jsonData,L"AttachmentNames[i].ViewedOn");
        ViewCount = CkJsonObjectW_IntOf(jsonData,L"AttachmentNames[i].ViewCount");
        Location = CkJsonObjectW_stringOf(jsonData,L"AttachmentNames[i].Location");
        i = i + 1;
    }

    i = 0;
    count_i = CkJsonObjectW_SizeOfArray(jsonData,L"AllRecipients");
    while (i < count_i) {
        CkJsonObjectW_putI(jsonData,i);
        Id = CkJsonObjectW_IntOf(jsonData,L"AllRecipients[i].Id");
        UserName = CkJsonObjectW_stringOf(jsonData,L"AllRecipients[i].UserName");
        AccountId = CkJsonObjectW_IntOf(jsonData,L"AllRecipients[i].AccountId");
        AccountName = CkJsonObjectW_stringOf(jsonData,L"AllRecipients[i].AccountName");
        AccountTitle = CkJsonObjectW_stringOf(jsonData,L"AllRecipients[i].AccountTitle");
        FirstName = CkJsonObjectW_stringOf(jsonData,L"AllRecipients[i].FirstName");
        LastName = CkJsonObjectW_stringOf(jsonData,L"AllRecipients[i].LastName");
        EmailAddress = CkJsonObjectW_stringOf(jsonData,L"AllRecipients[i].EmailAddress");
        LastSentOn = CkJsonObjectW_stringOf(jsonData,L"AllRecipients[i].LastSentOn");
        SendCount = CkJsonObjectW_IntOf(jsonData,L"AllRecipients[i].SendCount");
        IsFavorite = CkJsonObjectW_BoolOf(jsonData,L"AllRecipients[i].IsFavorite");
        i = i + 1;
    }



    CkHttpW_Dispose(http);
    CkHttpRequestW_Dispose(req);
    CkHttpResponseW_Dispose(resp);
    CkStringBuilderW_Dispose(sbResponseBody);
    CkJsonObjectW_Dispose(jResp);
    CkJsonObjectW_Dispose(jsonData);

    }