C
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 C Downloads
#include <C_CkHttp.h>
#include <C_CkHttpRequest.h>
#include <C_CkHttpResponse.h>
#include <C_CkStringBuilder.h>
#include <C_CkJsonObject.h>
void ChilkatSample(void)
{
BOOL success;
HCkHttp http;
HCkHttpRequest req;
HCkHttpResponse resp;
HCkStringBuilder sbResponseBody;
HCkJsonObject jResp;
int respStatusCode;
int ReturnCode;
const char *ReturnCodeText;
const char *Data;
HCkJsonObject jsonData;
int MessageId;
const char *Name;
const char *DisplayName;
int Size;
BOOL WasViewed;
const char *Location;
const char *UserName;
int AccountId;
const char *AccountName;
const char *AccountTitle;
const char *FirstName;
const char *LastName;
const char *LastSentOn;
int SendCount;
BOOL IsFavorite;
int Id;
int FromUserId;
int FromMailBoxId;
int FromUserType;
const char *FromUserName;
const char *FromUserFullName;
const char *FromUserAccountName;
const char *FromUserAccountTitle;
int ToUserId;
int ToUserType;
const char *ToUserMailboxId;
const char *ToUserName;
const char *ToUserFullName;
const char *EmailAddress;
const char *Password;
const char *Subject;
const char *PatientMedTunnelId;
const char *Body;
const char *DateReceived;
const char *DisplayDateReceived;
int ViewCount;
const char *ViewedOn;
int AttachmentCount;
int Status;
int ParentMessageId;
int DistributionListId;
const char *DistributionListName;
const char *BodyHistory;
const char *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 = CkHttp_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 = CkHttpRequest_Create();
CkHttpRequest_putHttpVerb(req,"POST");
CkHttpRequest_putPath(req,"/MedTunnelMsg/api/Message/SendMessage");
CkHttpRequest_putContentType(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");
success = CkHttpRequest_AddFileForUpload2(req,"file1","qa_data/jpg/starfish.jpg","application/octet-stream");
CkHttpRequest_AddHeader(req,"Expect","100-continue");
resp = CkHttpResponse_Create();
success = CkHttp_HttpSReq(http,"server.medtunnel.com",443,TRUE,req,resp);
if (success == FALSE) {
printf("%s\n",CkHttp_lastErrorText(http));
CkHttp_Dispose(http);
CkHttpRequest_Dispose(req);
CkHttpResponse_Dispose(resp);
return;
}
sbResponseBody = CkStringBuilder_Create();
CkHttpResponse_GetBodySb(resp,sbResponseBody);
jResp = CkJsonObject_Create();
CkJsonObject_LoadSb(jResp,sbResponseBody);
CkJsonObject_putEmitCompact(jResp,FALSE);
printf("Response Body:\n");
printf("%s\n",CkJsonObject_emit(jResp));
respStatusCode = CkHttpResponse_getStatusCode(resp);
printf("Response Status Code = %d\n",respStatusCode);
if (respStatusCode >= 400) {
printf("Response Header:\n");
printf("%s\n",CkHttpResponse_header(resp));
printf("Failed.\n");
CkHttp_Dispose(http);
CkHttpRequest_Dispose(req);
CkHttpResponse_Dispose(resp);
CkStringBuilder_Dispose(sbResponseBody);
CkJsonObject_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 = CkJsonObject_IntOf(jResp,"ReturnCode");
ReturnCodeText = CkJsonObject_stringOf(jResp,"ReturnCodeText");
Data = CkJsonObject_stringOf(jResp,"Data");
// Load the Data into another JSON object and parse..
jsonData = CkJsonObject_Create();
CkJsonObject_Load(jsonData,Data);
CkJsonObject_putEmitCompact(jsonData,FALSE);
printf("%s\n",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
// }
// 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 = CkJsonObject_IntOf(jsonData,"Id");
FromUserId = CkJsonObject_IntOf(jsonData,"FromUserId");
FromMailBoxId = CkJsonObject_IntOf(jsonData,"FromMailBoxId");
FromUserType = CkJsonObject_IntOf(jsonData,"FromUserType");
FromUserName = CkJsonObject_stringOf(jsonData,"FromUserName");
FromUserFullName = CkJsonObject_stringOf(jsonData,"FromUserFullName");
FromUserAccountName = CkJsonObject_stringOf(jsonData,"FromUserAccountName");
FromUserAccountTitle = CkJsonObject_stringOf(jsonData,"FromUserAccountTitle");
ToUserId = CkJsonObject_IntOf(jsonData,"ToUserId");
ToUserType = CkJsonObject_IntOf(jsonData,"ToUserType");
ToUserMailboxId = CkJsonObject_stringOf(jsonData,"ToUserMailboxId");
ToUserName = CkJsonObject_stringOf(jsonData,"ToUserName");
ToUserFullName = CkJsonObject_stringOf(jsonData,"ToUserFullName");
EmailAddress = CkJsonObject_stringOf(jsonData,"EmailAddress");
Password = CkJsonObject_stringOf(jsonData,"Password");
Subject = CkJsonObject_stringOf(jsonData,"Subject");
PatientMedTunnelId = CkJsonObject_stringOf(jsonData,"PatientMedTunnelId");
Body = CkJsonObject_stringOf(jsonData,"Body");
DateReceived = CkJsonObject_stringOf(jsonData,"DateReceived");
DisplayDateReceived = CkJsonObject_stringOf(jsonData,"DisplayDateReceived");
ViewCount = CkJsonObject_IntOf(jsonData,"ViewCount");
ViewedOn = CkJsonObject_stringOf(jsonData,"ViewedOn");
AttachmentCount = CkJsonObject_IntOf(jsonData,"AttachmentCount");
Status = CkJsonObject_IntOf(jsonData,"Status");
ParentMessageId = CkJsonObject_IntOf(jsonData,"ParentMessageId");
DistributionListId = CkJsonObject_IntOf(jsonData,"DistributionListId");
DistributionListName = CkJsonObject_stringOf(jsonData,"DistributionListName");
BodyHistory = CkJsonObject_stringOf(jsonData,"BodyHistory");
ReadReceiptCallbackUrl = CkJsonObject_stringOf(jsonData,"ReadReceiptCallbackUrl");
SendGlobalNotifications = CkJsonObject_BoolOf(jsonData,"SendGlobalNotifications");
i = 0;
count_i = CkJsonObject_SizeOfArray(jsonData,"AttachmentNames");
while (i < count_i) {
CkJsonObject_putI(jsonData,i);
MessageId = CkJsonObject_IntOf(jsonData,"AttachmentNames[i].MessageId");
Id = CkJsonObject_IntOf(jsonData,"AttachmentNames[i].Id");
Name = CkJsonObject_stringOf(jsonData,"AttachmentNames[i].Name");
DisplayName = CkJsonObject_stringOf(jsonData,"AttachmentNames[i].DisplayName");
Size = CkJsonObject_IntOf(jsonData,"AttachmentNames[i].Size");
WasViewed = CkJsonObject_BoolOf(jsonData,"AttachmentNames[i].WasViewed");
ViewedOn = CkJsonObject_stringOf(jsonData,"AttachmentNames[i].ViewedOn");
ViewCount = CkJsonObject_IntOf(jsonData,"AttachmentNames[i].ViewCount");
Location = CkJsonObject_stringOf(jsonData,"AttachmentNames[i].Location");
i = i + 1;
}
i = 0;
count_i = CkJsonObject_SizeOfArray(jsonData,"AllRecipients");
while (i < count_i) {
CkJsonObject_putI(jsonData,i);
Id = CkJsonObject_IntOf(jsonData,"AllRecipients[i].Id");
UserName = CkJsonObject_stringOf(jsonData,"AllRecipients[i].UserName");
AccountId = CkJsonObject_IntOf(jsonData,"AllRecipients[i].AccountId");
AccountName = CkJsonObject_stringOf(jsonData,"AllRecipients[i].AccountName");
AccountTitle = CkJsonObject_stringOf(jsonData,"AllRecipients[i].AccountTitle");
FirstName = CkJsonObject_stringOf(jsonData,"AllRecipients[i].FirstName");
LastName = CkJsonObject_stringOf(jsonData,"AllRecipients[i].LastName");
EmailAddress = CkJsonObject_stringOf(jsonData,"AllRecipients[i].EmailAddress");
LastSentOn = CkJsonObject_stringOf(jsonData,"AllRecipients[i].LastSentOn");
SendCount = CkJsonObject_IntOf(jsonData,"AllRecipients[i].SendCount");
IsFavorite = CkJsonObject_BoolOf(jsonData,"AllRecipients[i].IsFavorite");
i = i + 1;
}
CkHttp_Dispose(http);
CkHttpRequest_Dispose(req);
CkHttpResponse_Dispose(resp);
CkStringBuilder_Dispose(sbResponseBody);
CkJsonObject_Dispose(jResp);
CkJsonObject_Dispose(jsonData);
}