Sample code for 30+ languages & platforms
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 C Downloads

C
#include <C_CkRest.h>

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

    success = FALSE;

    rest = CkRest_Create();
    bTls = TRUE;
    bAutoReconnect = TRUE;
    success = CkRest_Connect(rest,"example.com",443,bTls,bAutoReconnect);
    if (success == FALSE) {
        printf("%s\n",CkRest_lastErrorText(rest));
        CkRest_Dispose(rest);
        return;
    }

    success = CkRest_SendReqNoBody(rest,"GET","/api/items/123");
    if (success == FALSE) {
        printf("%s\n",CkRest_lastErrorText(rest));
        CkRest_Dispose(rest);
        return;
    }

    statusCode = CkRest_ReadResponseHeader(rest);
    if (statusCode < 0) {
        printf("%s\n",CkRest_lastErrorText(rest));
        CkRest_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 = CkRest_getNumResponseHeaders(rest);

    for (i = 0; i <= numHeaders - 1; i++) {
        name = CkRest_responseHdrName(rest,i);
        if (CkRest_getLastMethodSuccess(rest) == FALSE) {
            printf("%s\n",CkRest_lastErrorText(rest));
            CkRest_Dispose(rest);
            return;
        }

        value = CkRest_responseHdrValue(rest,i);
        if (CkRest_getLastMethodSuccess(rest) == FALSE) {
            printf("%s\n",CkRest_lastErrorText(rest));
            CkRest_Dispose(rest);
            return;
        }

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



    CkRest_Dispose(rest);

    }