Unicode C++ Requires Chilkat v11.0.0+
Unicode C++
Download the media files from a Twilio MMS
See more Twilio Examples
Supply the Message SID of a received or sent MMS. List its media records, then request each media resource without the .json extension to download the file bytes. Twilio may return a temporary HTTPS download URL. The example uses a separate HTTP client for that URL so the Twilio API key stays with the API host.
Run in an empty output directory. Files are written as mms-media-1.bin, mms-media-2.bin, and so on. The code prints the media SID and MIME type alongside each filename. An empty media list produces a count of zero.
Chilkat Unicode C++ Downloads
#include <CkHttpW.h>
#include <CkStringBuilderW.h>
#include <CkHttpResponseW.h>
#include <CkJsonObjectW.h>
void ChilkatSample(void)
{
bool success = false;
// Assumes Chilkat is unlocked. See Global Unlock Sample.
// Load an API key for the Twilio account from private application configuration.
CkHttpW http;
http.put_RequireSslCertVerify(true);
http.put_BasicAuth(true);
http.put_Login(L"YOUR_API_KEY_SID");
http.put_Password(L"YOUR_API_KEY_SECRET");
CkStringBuilderW url;
url.Append(L"https://api.twilio.com/2010-04-01/Accounts/");
url.Append(L"YOUR_ACCOUNT_SID");
url.Append(L"/Messages");
url.Append(L"/");
url.Append(L"YOUR_MESSAGE_SID");
url.Append(L"/Media");
CkStringBuilderW mediaBase;
mediaBase.AppendSb(url);
url.Append(L".json");
// Handle the API redirect explicitly so a signed download uses no Twilio credentials.
http.put_FollowRedirects(false);
CkHttpW download;
download.put_RequireSslCertVerify(true);
CkHttpResponseW fileResp;
CkStringBuilderW fileUrl;
CkStringBuilderW filename;
CkStringBuilderW mediaSid;
int saved = 0;
CkHttpResponseW resp;
CkJsonObjectW reply;
bool more = true;
while ((more == true)) {
success = http.HttpNoBody(L"GET",url.getAsString(),resp);
if (resp.get_StatusCode() == 0) {
wprintf(L"No HTTP response from Twilio.\n");
return;
}
bool parsed = reply.Load(resp.bodyStr());
if ((success != true) || (resp.get_StatusCode() != 200)) {
wprintf(L"Twilio returned HTTP %d\n",resp.get_StatusCode());
if ((parsed == true) && (reply.JsonTypeOf(L"code") == 2)) {
wprintf(L"Twilio error: %d %s\n",reply.IntOf(L"code"),reply.stringOf(L"message"));
}
return;
}
if (parsed != true) {
wprintf(L"Expected a JSON response from Twilio.\n");
return;
}
if (reply.JsonTypeOf(L"media_list") != 4) {
wprintf(L"Expected an array of media items.\n");
return;
}
int count = reply.SizeOfArray(L"media_list");
int i = 0;
while ((i < count)) {
reply.put_I(i);
mediaSid.SetString(reply.stringOf(L"media_list[i].sid"));
if (mediaSid.get_Length() == 0) {
wprintf(L"Media item is missing its SID.\n");
return;
}
fileUrl.SetString(mediaBase.getAsString());
fileUrl.Append(L"/");
fileUrl.AppendSb(mediaSid);
success = http.HttpNoBody(L"GET",fileUrl.getAsString(),fileResp);
if ((fileResp.get_StatusCode() >= 300) && (fileResp.get_StatusCode() < 400)) {
fileUrl.SetString(fileResp.getHeaderField(L"Location"));
if (fileUrl.StartsWith(L"https://",false) != true) {
wprintf(L"Expected an HTTPS media download URL.\n");
return;
}
success = download.HttpNoBody(L"GET",fileUrl.getAsString(),fileResp);
}
if ((success != true) || (fileResp.get_StatusCode() != 200)) {
wprintf(L"Media download failed for %s. HTTP %d\n",mediaSid.getAsString(),fileResp.get_StatusCode());
return;
}
filename.SetString(L"mms-media-");
filename.AppendInt(saved + 1);
filename.Append(L".bin");
success = fileResp.SaveBodyBinary(filename.getAsString());
if (success == false) {
wprintf(L"%s\n",fileResp.lastErrorText());
return;
}
wprintf(L"%s %s %s\n",filename.getAsString(),mediaSid.getAsString(),reply.stringOf(L"media_list[i].content_type"));
saved = saved + 1;
i = i + 1;
}
if (reply.IsNullOf(L"next_page_uri") == true) {
more = false;
}
else {
url.SetString(reply.stringOf(L"next_page_uri"));
if (url.StartsWith(L"/2010-04-01/Accounts/",true) != true) {
wprintf(L"Expected a Twilio relative next-page URI.\n");
return;
}
url.Prepend(L"https://api.twilio.com");
}
}
wprintf(L"Media files saved: %d\n",saved);
}