Sample code for 30+ languages & platforms
C

Get a REST Response Header by Name

See more REST Examples

Demonstrates Rest.ResponseHdrByName, which returns the value of a response header field by name. Header field names are case-insensitive.

Background. Response headers are available after ReadResponseHeader has read the status line and headers. Lookup by name is convenient when a specific header, such as Content-Type, is needed.

Chilkat C Downloads

C
#include <C_CkRest.h>

void ChilkatSample(void)
    {
    BOOL success;
    HCkRest rest;
    BOOL bTls;
    BOOL bAutoReconnect;
    int statusCode;
    const char *contentType;

    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 a response header field by name.  Header field names are case-insensitive.
    contentType = CkRest_responseHdrByName(rest,"Content-Type");
    if (CkRest_getLastMethodSuccess(rest) == FALSE) {
        printf("%s\n",CkRest_lastErrorText(rest));
        CkRest_Dispose(rest);
        return;
    }

    printf("Content-Type: %s\n",contentType);


    CkRest_Dispose(rest);

    }