Sample code for 30+ languages & platforms
Unicode C

Enumerate REST Response Header Names

See more REST Examples

Demonstrates Rest.ResponseHdrName, which returns the field name of the response header at a zero-based index. The example enumerates all headers, pairing each name with its value.

Background. Response headers can be enumerated by index from 0 through NumResponseHeaders minus one. ResponseHdrName and ResponseHdrValue use the same index for a given header.

Chilkat Unicode C Downloads

Unicode C
#include <C_CkRestW.h>

void ChilkatSample(void)
    {
    BOOL success;
    HCkRestW rest;
    BOOL bTls;
    BOOL bAutoReconnect;
    int statusCode;
    int numHeaders;
    int i;
    const wchar_t *name;
    const wchar_t *value;

    success = FALSE;

    rest = CkRestW_Create();
    bTls = TRUE;
    bAutoReconnect = TRUE;
    success = CkRestW_Connect(rest,L"example.com",443,bTls,bAutoReconnect);
    if (success == FALSE) {
        wprintf(L"%s\n",CkRestW_lastErrorText(rest));
        CkRestW_Dispose(rest);
        return;
    }

    success = CkRestW_SendReqNoBody(rest,L"GET",L"/api/items/123");
    if (success == FALSE) {
        wprintf(L"%s\n",CkRestW_lastErrorText(rest));
        CkRestW_Dispose(rest);
        return;
    }

    statusCode = CkRestW_ReadResponseHeader(rest);
    if (statusCode < 0) {
        wprintf(L"%s\n",CkRestW_lastErrorText(rest));
        CkRestW_Dispose(rest);
        return;
    }

    //  Enumerate the response header fields by zero-based index.  ResponseHdrName returns the field name
    //  and ResponseHdrValue returns the value at the same index.
    numHeaders = CkRestW_getNumResponseHeaders(rest);

    for (i = 0; i <= numHeaders - 1; i++) {
        name = CkRestW_responseHdrName(rest,i);
        if (CkRestW_getLastMethodSuccess(rest) == FALSE) {
            wprintf(L"%s\n",CkRestW_lastErrorText(rest));
            CkRestW_Dispose(rest);
            return;
        }

        value = CkRestW_responseHdrValue(rest,i);
        if (CkRestW_getLastMethodSuccess(rest) == FALSE) {
            wprintf(L"%s\n",CkRestW_lastErrorText(rest));
            CkRestW_Dispose(rest);
            return;
        }

        wprintf(L"%s: %s\n",name,value);
    }



    CkRestW_Dispose(rest);

    }