1<html> 2<head> 3<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 4<title>HEAD request (Client) </title> 5<link rel="stylesheet" href="../../../../../../doc/src/boostbook.css" type="text/css"> 6<meta name="generator" content="DocBook XSL Stylesheets V1.79.1"> 7<link rel="home" href="../../index.html" title="Chapter 1. Boost.Beast"> 8<link rel="up" href="../more_examples.html" title="HTTP Examples"> 9<link rel="prev" href="expect_100_continue_server.html" title="Expect 100-continue (Server) "> 10<link rel="next" href="head_response_server.html" title="HEAD response (Server) "> 11</head> 12<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"> 13<table cellpadding="2" width="100%"><tr> 14<td valign="top"><img alt="Boost C++ Libraries" width="277" height="86" src="../../../../../../boost.png"></td> 15<td align="center"><a href="../../../../../../index.html">Home</a></td> 16<td align="center"><a href="../../../../../../libs/libraries.htm">Libraries</a></td> 17<td align="center"><a href="http://www.boost.org/users/people.html">People</a></td> 18<td align="center"><a href="http://www.boost.org/users/faq.html">FAQ</a></td> 19<td align="center"><a href="../../../../../../more/index.htm">More</a></td> 20</tr></table> 21<hr> 22<div class="spirit-nav"> 23<a accesskey="p" href="expect_100_continue_server.html"><img src="../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../more_examples.html"><img src="../../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../../index.html"><img src="../../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="head_response_server.html"><img src="../../../../../../doc/src/images/next.png" alt="Next"></a> 24</div> 25<div class="section"> 26<div class="titlepage"><div><div><h3 class="title"> 27<a name="beast.more_examples.head_request_client"></a><a class="link" href="head_request_client.html" title="HEAD request (Client) ">HEAD request 28 (Client) </a> 29</h3></div></div></div> 30<p> 31 The <a href="https://tools.ietf.org/html/rfc7231#section-4.3.2" target="_top">HEAD request</a> 32 method indicates to the server that the client wishes to receive the entire 33 header that would be delivered if the method was GET, except that the body 34 is omitted. When a client wishes to receive the response to a HEAD request, 35 it is necessary to inform the parser not to expect a body. This is done by 36 calling <a class="link" href="../ref/boost__beast__http__basic_parser/skip.html" title="http::basic_parser::skip"><code class="computeroutput"><span class="identifier">basic_parser</span><span class="special">::</span><span class="identifier">skip</span></code></a> with the value <code class="computeroutput"><span class="keyword">true</span></code>, as shown in this example: 37 </p> 38<pre class="programlisting"><span class="comment">/** Send a HEAD request for a resource. 39 40 This function submits a HEAD request for the specified resource 41 and returns the response. 42 43 @param res The response. This is an output parameter. 44 45 @param stream The synchronous stream to use. 46 47 @param buffer The buffer to use. 48 49 @param target The request target. 50 51 @param ec Set to the error, if any occurred. 52 53 @throws std::invalid_argument if target is empty. 54*/</span> 55<span class="keyword">template</span><span class="special"><</span> 56 <span class="keyword">class</span> <span class="identifier">SyncStream</span><span class="special">,</span> 57 <span class="keyword">class</span> <span class="identifier">DynamicBuffer</span> 58<span class="special">></span> 59<span class="identifier">response</span><span class="special"><</span><span class="identifier">empty_body</span><span class="special">></span> 60<span class="identifier">do_head_request</span><span class="special">(</span> 61 <span class="identifier">SyncStream</span><span class="special">&</span> <span class="identifier">stream</span><span class="special">,</span> 62 <span class="identifier">DynamicBuffer</span><span class="special">&</span> <span class="identifier">buffer</span><span class="special">,</span> 63 <span class="identifier">string_view</span> <span class="identifier">target</span><span class="special">,</span> 64 <span class="identifier">error_code</span><span class="special">&</span> <span class="identifier">ec</span><span class="special">)</span> 65<span class="special">{</span> 66 <span class="comment">// Do some type checking to be a good citizen</span> 67 <span class="keyword">static_assert</span><span class="special">(</span><span class="identifier">is_sync_stream</span><span class="special"><</span><span class="identifier">SyncStream</span><span class="special">>::</span><span class="identifier">value</span><span class="special">,</span> 68 <span class="string">"SyncStream requirements not met"</span><span class="special">);</span> 69 <span class="keyword">static_assert</span><span class="special">(</span> 70 <span class="identifier">boost</span><span class="special">::</span><span class="identifier">asio</span><span class="special">::</span><span class="identifier">is_dynamic_buffer</span><span class="special"><</span><span class="identifier">DynamicBuffer</span><span class="special">>::</span><span class="identifier">value</span><span class="special">,</span> 71 <span class="string">"DynamicBuffer requirements not met"</span><span class="special">);</span> 72 73 <span class="comment">// The interfaces we are using are low level and do not</span> 74 <span class="comment">// perform any checking of arguments; so we do it here.</span> 75 <span class="keyword">if</span><span class="special">(</span><span class="identifier">target</span><span class="special">.</span><span class="identifier">empty</span><span class="special">())</span> 76 <span class="keyword">throw</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">invalid_argument</span><span class="special">(</span><span class="string">"target may not be empty"</span><span class="special">);</span> 77 78 <span class="comment">// Build the HEAD request for the target</span> 79 <span class="identifier">request</span><span class="special"><</span><span class="identifier">empty_body</span><span class="special">></span> <span class="identifier">req</span><span class="special">;</span> 80 <span class="identifier">req</span><span class="special">.</span><span class="identifier">version</span><span class="special">(</span><span class="number">11</span><span class="special">);</span> 81 <span class="identifier">req</span><span class="special">.</span><span class="identifier">method</span><span class="special">(</span><span class="identifier">verb</span><span class="special">::</span><span class="identifier">head</span><span class="special">);</span> 82 <span class="identifier">req</span><span class="special">.</span><span class="identifier">target</span><span class="special">(</span><span class="identifier">target</span><span class="special">);</span> 83 <span class="identifier">req</span><span class="special">.</span><span class="identifier">set</span><span class="special">(</span><span class="identifier">field</span><span class="special">::</span><span class="identifier">user_agent</span><span class="special">,</span> <span class="string">"test"</span><span class="special">);</span> 84 85 <span class="comment">// A client MUST send a Host header field in all HTTP/1.1 request messages.</span> 86 <span class="comment">// https://tools.ietf.org/html/rfc7230#section-5.4</span> 87 <span class="identifier">req</span><span class="special">.</span><span class="identifier">set</span><span class="special">(</span><span class="identifier">field</span><span class="special">::</span><span class="identifier">host</span><span class="special">,</span> <span class="string">"localhost"</span><span class="special">);</span> 88 89 <span class="comment">// Now send it</span> 90 <span class="identifier">write</span><span class="special">(</span><span class="identifier">stream</span><span class="special">,</span> <span class="identifier">req</span><span class="special">,</span> <span class="identifier">ec</span><span class="special">);</span> 91 <span class="keyword">if</span><span class="special">(</span><span class="identifier">ec</span><span class="special">)</span> 92 <span class="keyword">return</span> <span class="special">{};</span> 93 94 <span class="comment">// Create a parser to read the response.</span> 95 <span class="comment">// We use the `empty_body` type since</span> 96 <span class="comment">// a response to a HEAD request MUST NOT</span> 97 <span class="comment">// include a body.</span> 98 <span class="identifier">response_parser</span><span class="special"><</span><span class="identifier">empty_body</span><span class="special">></span> <span class="identifier">p</span><span class="special">;</span> 99 100 <span class="comment">// Inform the parser that there will be no body.</span> 101 <span class="identifier">p</span><span class="special">.</span><span class="identifier">skip</span><span class="special">(</span><span class="keyword">true</span><span class="special">);</span> 102 103 <span class="comment">// Read the message. Even though fields like</span> 104 <span class="comment">// Content-Length or Transfer-Encoding may be</span> 105 <span class="comment">// set, the message will not contain a body.</span> 106 <span class="identifier">read</span><span class="special">(</span><span class="identifier">stream</span><span class="special">,</span> <span class="identifier">buffer</span><span class="special">,</span> <span class="identifier">p</span><span class="special">,</span> <span class="identifier">ec</span><span class="special">);</span> 107 <span class="keyword">if</span><span class="special">(</span><span class="identifier">ec</span><span class="special">)</span> 108 <span class="keyword">return</span> <span class="special">{};</span> 109 110 <span class="comment">// Transfer ownership of the response to the caller.</span> 111 <span class="keyword">return</span> <span class="identifier">p</span><span class="special">.</span><span class="identifier">release</span><span class="special">();</span> 112<span class="special">}</span> 113</pre> 114</div> 115<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr> 116<td align="left"></td> 117<td align="right"><div class="copyright-footer">Copyright © 2016-2019 Vinnie 118 Falco<p> 119 Distributed under the Boost Software License, Version 1.0. (See accompanying 120 file LICENSE_1_0.txt or copy at <a href="http://www.boost.org/LICENSE_1_0.txt" target="_top">http://www.boost.org/LICENSE_1_0.txt</a>) 121 </p> 122</div></td> 123</tr></table> 124<hr> 125<div class="spirit-nav"> 126<a accesskey="p" href="expect_100_continue_server.html"><img src="../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../more_examples.html"><img src="../../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../../index.html"><img src="../../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="head_response_server.html"><img src="../../../../../../doc/src/images/next.png" alt="Next"></a> 127</div> 128</body> 129</html> 130