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

void ChilkatSample(void)
    {
    bool success = false;

    CkRest rest;
    bool bTls = true;
    bool bAutoReconnect = true;
    success = rest.Connect("example.com",443,bTls,bAutoReconnect);
    if (success == false) {
        std::cout << rest.lastErrorText() << "\r\n";
        return;
    }

    success = rest.SendReqNoBody("GET","/api/items/123");
    if (success == false) {
        std::cout << rest.lastErrorText() << "\r\n";
        return;
    }

    int statusCode = rest.ReadResponseHeader();
    if (statusCode < 0) {
        std::cout << rest.lastErrorText() << "\r\n";
        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 char *name = rest.responseHdrName(i);
        if (rest.get_LastMethodSuccess() == false) {
            std::cout << rest.lastErrorText() << "\r\n";
            return;
        }

        const char *value = rest.responseHdrValue(i);
        if (rest.get_LastMethodSuccess() == false) {
            std::cout << rest.lastErrorText() << "\r\n";
            return;
        }

        std::cout << name << ": " << value << "\r\n";
    }
    }