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

    //  Return the value of a response header field by name.  Header field names are case-insensitive.
    const char *contentType = rest.responseHdrByName("Content-Type");
    if (rest.get_LastMethodSuccess() == false) {
        std::cout << rest.lastErrorText() << "\r\n";
        return;
    }

    std::cout << "Content-Type: " << contentType << "\r\n";
    }