include/boost/burl/string.hpp

100.0% Lines (2/2) 100.0% List of functions (2/2) 100.0% Branches (1/1)
string.hpp
f(x) Functions (2)
Line Branch TLA Hits Source Code
1 //
2 // Copyright (c) 2026 Mohammad Nejati
3 //
4 // Distributed under the Boost Software License, Version 1.0. (See accompanying
5 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
6 //
7 // Official repository: https://github.com/cppalliance/burl
8 //
9
10 #ifndef BOOST_BURL_STRING_HPP
11 #define BOOST_BURL_STRING_HPP
12
13 #include <boost/burl/any_request_body.hpp>
14 #include <boost/burl/conversion.hpp>
15 #include <boost/burl/detail/config.hpp>
16 #include <boost/burl/response.hpp>
17
18 #include <boost/capy/io_task.hpp>
19
20 #include <cstddef>
21 #include <string>
22 #include <string_view>
23
24 namespace boost
25 {
26 namespace burl
27 {
28
29 /** Create a request body from a string.
30
31 The body takes ownership of the string. The
32 `Content-Type` is `text/plain; charset=utf-8`,
33 and the `Content-Length` is the size of the
34 string.
35
36 @par Example
37 @code
38 auto r = co_await c.post(url)
39 .body(std::string("payload"))
40 .send();
41 @endcode
42
43 @param body The string to send.
44
45 @return The request body.
46 */
47 BOOST_BURL_DECL
48 any_request_body
49 tag_invoke(body_from_tag<std::string>, std::string body);
50
51 /** Create a request body from a string view.
52
53 The body refers to the passed characters;
54 ownership is not transferred. The caller is
55 responsible for ensuring that the lifetime of
56 the underlying character buffer extends until
57 the request has been sent. The `Content-Type`
58 is `text/plain; charset=utf-8`, and the
59 `Content-Length` is the size of the view.
60
61 @param body The string view to send.
62
63 @return The request body.
64 */
65 BOOST_BURL_DECL
66 any_request_body
67 tag_invoke(body_from_tag<std::string_view>, std::string_view body);
68
69 /** Create a request body from a character array.
70
71 The array is treated as a string literal; the
72 terminating null character is excluded. The
73 body refers to the passed characters; ownership
74 is not transferred. The caller is responsible
75 for ensuring that the lifetime of the array
76 extends until the request has been sent.
77
78 @par Example
79 @code
80 auto r = co_await c.post(url)
81 .body("payload")
82 .send();
83 @endcode
84
85 @param body The character array to send.
86
87 @return The request body.
88 */
89 template<std::size_t N>
90 any_request_body
91 3x tag_invoke(body_from_tag<char[N]>, const char (&body)[N])
92 {
93
1/1
✓ Branch 2 taken 3 times.
3x return tag_invoke(body_from_tag<std::string_view>{}, { body, N - 1 });
94 }
95
96 /** Asynchronously read a response body into a string.
97
98 This overload enables `resp.as<std::string>()`
99 and related functions, which read the entire
100 body into a `std::string`.
101
102 @par Example
103 @code
104 auto r = co_await c.get("https://example.com")
105 .as<std::string>();
106 @endcode
107
108 @param resp The response to read from.
109
110 @return An awaitable yielding
111 `(error_code,std::string)`.
112 */
113 BOOST_BURL_DECL
114 capy::io_task<std::string>
115 tag_invoke(body_to_tag<std::string>, response& resp);
116
117 } // namespace burl
118 } // namespace boost
119
120 #endif
121