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 <CkRestW.h>

void ChilkatSample(void)
    {
    bool success = false;

    CkRestW rest;
    bool bTls = true;
    bool bAutoReconnect = true;
    success = rest.Connect(L"example.com",443,bTls,bAutoReconnect);
    if (success == false) {
        wprintf(L"%s\n",rest.lastErrorText());
        return;
    }

    success = rest.SendReqNoBody(L"GET",L"/api/items/123");
    if (success == false) {
        wprintf(L"%s\n",rest.lastErrorText());
        return;
    }

    int statusCode = rest.ReadResponseHeader();
    if (statusCode < 0) {
        wprintf(L"%s\n",rest.lastErrorText());
        return;
    }

    //  Enumerate the response header fields by zero-based index.  ResponseHdrName returns the field name
    //  and ResponseHdrValue returns the value at the same index.
    int numHeaders = rest.get_NumResponseHeaders();
    int i;
    for (i = 0; i <= numHeaders - 1; i++) {
        const wchar_t *name = rest.responseHdrName(i);
        if (rest.get_LastMethodSuccess() == false) {
            wprintf(L"%s\n",rest.lastErrorText());
            return;
        }

        const wchar_t *value = rest.responseHdrValue(i);
        if (rest.get_LastMethodSuccess() == false) {
            wprintf(L"%s\n",rest.lastErrorText());
            return;
        }

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