Sample code for 30+ languages & platforms
Unicode C

Google Drive Multipart Upload String

See more REST Examples

Demonstrates a file upload to Google Drive where the contents of the file are contained in a string variable.

Chilkat Unicode C Downloads

Unicode C
#include <C_CkAuthGoogleW.h>
#include <C_CkRestW.h>
#include <C_CkJsonObjectW.h>

void ChilkatSample(void)
    {
    BOOL success;
    HCkAuthGoogleW gAuth;
    HCkRestW rest;
    BOOL bAutoReconnect;
    HCkJsonObjectW json;
    const wchar_t *jsonResponse;

    success = FALSE;

    // This example will upload a file to Google Drive.
    success = TRUE;

    // It requires the Chilkat API to have been previously unlocked.
    // See Global Unlock Sample for sample code.

    gAuth = CkAuthGoogleW_Create();
    CkAuthGoogleW_putScope(gAuth,L"https://www.googleapis.com/auth/drive");
    CkAuthGoogleW_putSubEmailAddress(gAuth,L"some.user@example.com");
    CkAuthGoogleW_putExpireNumSeconds(gAuth,3600);

    // Obtain an access token as shown in one of the following examples:
    // See Get Access Token using a Service Account JSON Key
    // See Get Access Token using a P12 File

    rest = CkRestW_Create();

    // Connect using TLS.
    bAutoReconnect = TRUE;
    success = CkRestW_Connect(rest,L"www.googleapis.com",443,TRUE,bAutoReconnect);

    // Provide the authentication credentials (i.e. the access key)
    success = CkRestW_SetAuthGoogle(rest,gAuth);

    // A multipart upload to Google Drive needs a multipart/related Content-Type
    success = CkRestW_AddHeader(rest,L"Content-Type",L"multipart/related");

    // Specify each part of the request.

    // The 1st part is JSON with information about the file.
    CkRestW_putPartSelector(rest,L"1");
    success = CkRestW_AddHeader(rest,L"Content-Type",L"application/json; charset=UTF-8");

    json = CkJsonObjectW_Create();
    success = CkJsonObjectW_AddStringAt(json,-1,L"title",L"helloWorld.txt");
    success = CkJsonObjectW_AddStringAt(json,-1,L"description",L"A simple text file that says Hello World.");
    success = CkJsonObjectW_AddStringAt(json,-1,L"mimeType",L"text/plain");
    success = CkRestW_SetMultipartBodyString(rest,CkJsonObjectW_emit(json));

    // The 2nd part is the file content.
    // In this case, we'll upload a simple text file containing "Hello World!"
    CkRestW_putPartSelector(rest,L"2");
    success = CkRestW_AddHeader(rest,L"Content-Type",L"text/plain");
    success = CkRestW_SetMultipartBodyString(rest,L"Hello World!");

    // POST https://www.googleapis.com/upload/drive/v2/files
    jsonResponse = CkRestW_fullRequestMultipart(rest,L"POST",L"/upload/drive/v2/files?uploadType=multipart");
    if (CkRestW_getLastMethodSuccess(rest) != TRUE) {
        wprintf(L"%s\n",CkRestW_lastErrorText(rest));
        CkAuthGoogleW_Dispose(gAuth);
        CkRestW_Dispose(rest);
        CkJsonObjectW_Dispose(json);
        return;
    }

    // Show the JSON response.
    wprintf(L"Response Status Code: %d\n",CkRestW_getResponseStatusCode(rest));
    wprintf(L"Json Response: %s\n",jsonResponse);


    CkAuthGoogleW_Dispose(gAuth);
    CkRestW_Dispose(rest);
    CkJsonObjectW_Dispose(json);

    }