Beast Logo


HEAD request (Client)

The HEAD request method indicates to the server that the client wishes to receive the entire header that would be delivered if the method was GET, except that the body is omitted.

/** Send a HEAD request for a resource.

    This function submits a HEAD request for the specified resource
    and returns the response.

    @param res The response. This is an output parameter.

    @param stream The synchronous stream to use.

    @param buffer The buffer to use.

    @param target The request target.

    @param ec Set to the error, if any occurred.

    @throws std::invalid_argument if target is empty.
    class SyncStream,
    class DynamicBuffer
    SyncStream& stream,
    DynamicBuffer& buffer,
    string_view target,
    error_code& ec)
    // Do some type checking to be a good citizen
        "SyncStream requirements not met");
        "DynamicBuffer requirments not met");

    // The interfaces we are using are low level and do not
    // perform any checking of arguments; so we do it here.
        throw std::invalid_argument("target may not be empty");

    // Build the HEAD request for the target
    request<empty_body> req;
    req.version = 11;
    req.set(field::user_agent, "test");

    // A client MUST send a Host header field in all HTTP/1.1 request messages.
    req.set(field::host, "localhost");

    // Now send it
    write(stream, req, ec);
        return {};

    // Create a parser to read the response.
    // Responses to HEAD requests MUST NOT include
    // a body, so we use the `empty_body` type and
    // only attempt to read the header.
    parser<false, empty_body> p;
    read_header(stream, buffer, p, ec);
        return {};

    // Transfer ownership of the response to the caller.
    return p.release();