Sample code for 30+ languages & platforms
Unicode C

HTTP GET with Custom Header and OAuth2 Bearer Token

See more HTTP Examples

Demonstrate how to send a GET request with customer headers and an "Authorization: Bearer " header.

Chilkat Unicode C Downloads

Unicode C
#include <C_CkHttpW.h>
#include <C_CkStringBuilderW.h>
#include <C_CkJsonObjectW.h>

void ChilkatSample(void)
    {
    BOOL success;
    HCkHttpW http;
    const wchar_t *url;
    HCkStringBuilderW sb;
    HCkJsonObjectW json;

    success = FALSE;

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

    http = CkHttpW_Create();

    // Setting the AuthToken property causes the "Authorization: Bearer <token>" header to be adeded.
    CkHttpW_putAuthToken(http,L"Just_the_access_token_here");

    // Add one or more custom headers..
    CkHttpW_SetRequestHeader(http,L"X-Tenant-ID",L"value goes here");
    CkHttpW_SetRequestHeader(http,L"blah-blah-blah",L"value goes here");

    url = L"https://www.example.com/abc/123?x=something&y=someOtherThing";

    // Send the GET request and get the response body in the StringBuilder object.
    sb = CkStringBuilderW_Create();
    success = CkHttpW_QuickGetSb(http,url,sb);
    if (success != TRUE) {
        wprintf(L"%s\n",CkHttpW_lastErrorText(http));
        CkHttpW_Dispose(http);
        CkStringBuilderW_Dispose(sb);
        return;
    }

    wprintf(L"response status code: %d\n",CkHttpW_getLastStatus(http));
    wprintf(L"response body:\n");
    wprintf(L"%s\n",CkStringBuilderW_getAsString(sb));

    // If the response contains JSON, you can load it into a Chilkat JSON object...
    json = CkJsonObjectW_Create();
    CkJsonObjectW_LoadSb(json,sb);
    CkJsonObjectW_putEmitCompact(json,FALSE);
    wprintf(L"%s\n",CkJsonObjectW_emit(json));


    CkHttpW_Dispose(http);
    CkStringBuilderW_Dispose(sb);
    CkJsonObjectW_Dispose(json);

    }