Sample code for 30+ languages & platforms
C

Example: Http.SetUrlVar method

Demonstrates the HTTP SetUrlVar method.

Chilkat C Downloads

C
#include <C_CkHttp.h>
#include <C_CkStringBuilder.h>

void ChilkatSample(void)
    {
    BOOL success;
    HCkHttp http;
    const char *url;
    HCkStringBuilder sbJson;
    int statusCode;

    success = FALSE;

    http = CkHttp_Create();

    url = "https://finnhub.io/api/v1/quote?symbol={$symbol}&token={$api_key}";

    //  When the request is sent, the {$symbol} is replaced with "MSFT"
    //  and the {$api_key} is replaced with "1234567890ABCDEF"
    CkHttp_SetUrlVar(http,"symbol","MSFT");
    CkHttp_SetUrlVar(http,"api_key","1234567890ABCDEF");

    sbJson = CkStringBuilder_Create();
    success = CkHttp_QuickGetSb(http,url,sbJson);
    if (success == FALSE) {
        printf("%s\n",CkHttp_lastErrorText(http));
        CkHttp_Dispose(http);
        CkStringBuilder_Dispose(sbJson);
        return;
    }

    statusCode = CkHttp_getLastStatus(http);
    if (statusCode != 200) {
        printf("Status code: %d\n",statusCode);
        printf("Error Message:\n");
        printf("%s\n",CkStringBuilder_getAsString(sbJson));
    }
    else {
        printf("JSON Stock Quote:\n");
        printf("%s\n",CkStringBuilder_getAsString(sbJson));
    }

    //  Output:

    //  JSON Stock Quote:
    //  {"c":522.98,"d":0.5,"dp":0.0957,"h":524.51,"l":520.86,"o":524.28,"pc":522.48,"t":1755271948}


    CkHttp_Dispose(http);
    CkStringBuilder_Dispose(sbJson);

    }