Sample code for 30+ languages & platforms
C

Get a REST Response Header Value by Index

See more REST Examples

Demonstrates Rest.ResponseHdrValue, which returns the value of the response header at a zero-based index. The same index used with ResponseHdrName gives the corresponding field name.

Background. Response headers can be enumerated by index from 0 through NumResponseHeaders minus one, after ReadResponseHeader has been called.

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 *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;
    }

    //  Return the value of the response header at a zero-based index.  Use the same index with
    //  ResponseHdrName to get the corresponding field name.
    numHeaders = CkRest_getNumResponseHeaders(rest);

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

        printf("Header %d value: %s\n",i,value);
    }



    CkRest_Dispose(rest);

    }