[go: up one dir, main page]

surf/
one_off.rs

1use crate::http::Method;
2use crate::RequestBuilder;
3
4/// Perform a one-off `GET` request.
5///
6/// # About the HTTP Method
7///
8/// The GET method requests a representation of the specified resource. Requests using GET should
9/// only retrieve data.
10///
11/// [Read more on MDN]
12///
13/// [Read more on MDN]: https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/GET
14///
15/// # Panics
16///
17/// This will panic if a malformed URL is passed.
18///
19/// # Errors
20///
21/// Returns errors from the middleware, http backend, and network sockets.
22///
23/// # Examples
24///
25/// ```no_run
26/// # #[async_std::main]
27/// # async fn main() -> surf::Result<()> {
28/// let string = surf::get("https://httpbin.org/get").recv_string().await?;
29/// # Ok(()) }
30/// ```
31pub fn get(uri: impl AsRef<str>) -> RequestBuilder {
32    let uri = uri.as_ref().parse().unwrap();
33    RequestBuilder::new(Method::Get, uri)
34}
35
36/// Perform a one-off `HEAD` request.
37///
38/// # About the HTTP Method
39///
40/// The HTTP HEAD method requests the headers that are returned if the specified resource would be
41/// requested with an HTTP GET method. Such a request can be done before deciding to download a
42/// large resource to save bandwidth, for example.
43///
44/// A response to a HEAD method should not have a body. If so, it must be ignored. Even so, entity
45/// headers describing the content of the body, like Content-Length may be included in the
46/// response. They don't relate to the body of the HEAD response, which should be empty, but to the
47/// body of similar request using the GET method would have returned as a response.
48///
49/// If the result of a HEAD request shows that a cached resource after a GET request is now
50/// outdated, the cache is invalidated, even if no GET request has been made.
51///
52/// [Read more on MDN]
53///
54/// [Read more on MDN]: https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/HEAD
55///
56/// # Panics
57///
58/// This will panic if a malformed URL is passed.
59///
60/// # Errors
61///
62/// Returns errors from the middleware, http backend, and network sockets.
63///
64/// # Examples
65///
66/// ```no_run
67/// # #[async_std::main]
68/// # async fn main() -> surf::Result<()> {
69/// let string = surf::head("https://httpbin.org/head").recv_string().await?;
70/// # Ok(()) }
71/// ```
72pub fn head(uri: impl AsRef<str>) -> RequestBuilder {
73    let uri = uri.as_ref().parse().unwrap();
74    RequestBuilder::new(Method::Head, uri)
75}
76
77/// Perform a one-off `POST` request.
78///
79/// # About the HTTP Method
80///
81/// The HTTP POST method sends data to the server. The type of the body of the request is indicated
82/// by the Content-Type header.
83///
84/// The difference between PUT and POST is that PUT is idempotent: calling it once or several times
85/// successively has the same effect (that is no side effect), where successive identical POST may
86/// have additional effects, like passing an order several times.
87///
88/// A POST request is typically sent via an HTML form and results in a change on the server. In
89/// this case, the content type is selected by putting the adequate string in the enctype attribute
90/// of the `<form>` element or the formenctype attribute of the `<input>` or `<button>` elements:
91///
92/// ```txt
93/// application/x-www-form-urlencoded: the keys and values are encoded in key-value tuples separated by '&', with a '=' between the key and the value. Non-alphanumeric characters in both keys and values are percent encoded: this is the reason why this type is not suitable to use with binary data (use multipart/form-data instead)
94/// multipart/form-data: each value is sent as a block of data ("body part"), with a user agent-defined delimiter ("boundary") separating each part. The keys are given in the Content-Disposition header of each part.
95/// text/plain
96/// ```
97///
98/// When the POST request is sent via a method other than an HTML form — like via an XMLHttpRequest
99/// — the body can take any type. As described in the HTTP 1.1 specification, POST is designed to
100/// allow a uniform method to cover the following functions:
101///
102/// ```txt
103/// Annotation of existing resources
104/// Posting a message to a bulletin board, newsgroup, mailing list, or similar group of articles;
105/// Adding a new user through a signup modal;
106/// Providing a block of data, such as the result of submitting a form, to a data-handling process;
107/// Extending a database through an append operation.
108/// ```
109///
110/// [Read more on MDN]
111///
112/// [Read more on MDN]: https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/POST
113///
114/// # Panics
115///
116/// This will panic if a malformed URL is passed.
117///
118/// # Errors
119///
120/// Returns errors from the middleware, http backend, and network sockets.
121///
122/// # Examples
123///
124/// ```no_run
125/// # #[async_std::main]
126/// # async fn main() -> surf::Result<()> {
127/// let string = surf::post("https://httpbin.org/post").recv_string().await?;
128/// # Ok(()) }
129/// ```
130pub fn post(uri: impl AsRef<str>) -> RequestBuilder {
131    let uri = uri.as_ref().parse().unwrap();
132    RequestBuilder::new(Method::Post, uri)
133}
134
135/// Perform a one-off `PUT` request.
136///
137/// # About the HTTP Method
138///
139/// The HTTP PUT request method creates a new resource or replaces a representation of the target
140/// resource with the request payload.
141///
142/// The difference between PUT and POST is that PUT is idempotent: calling it once or several times
143/// successively has the same effect (that is no side effect), where successive identical POST may
144/// have additional effects, like passing an order several times.
145///
146/// [Read more on MDN]
147///
148/// [Read more on MDN]: https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/PUT
149///
150/// # Panics
151///
152/// This will panic if a malformed URL is passed.
153///
154/// # Errors
155///
156/// Returns errors from the middleware, http backend, and network sockets.
157///
158/// # Examples
159///
160/// ```no_run
161/// # #[async_std::main]
162/// # async fn main() -> surf::Result<()> {
163/// let string = surf::put("https://httpbin.org/put").recv_string().await?;
164/// # Ok(()) }
165/// ```
166pub fn put(uri: impl AsRef<str>) -> RequestBuilder {
167    let uri = uri.as_ref().parse().unwrap();
168    RequestBuilder::new(Method::Put, uri)
169}
170
171/// Perform a one-off `DELETE` request.
172///
173/// # About the HTTP Method
174///
175/// The HTTP DELETE request method deletes the specified resource.
176///
177/// [Read more on MDN]
178///
179/// [Read more on MDN]: https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/DELETE
180///
181/// # Panics
182///
183/// This will panic if a malformed URL is passed.
184///
185/// # Errors
186///
187/// Returns errors from the middleware, http backend, and network sockets.
188///
189/// # Examples
190///
191/// ```no_run
192/// # #[async_std::main]
193/// # async fn main() -> surf::Result<()> {
194/// let string = surf::delete("https://httpbin.org/delete").recv_string().await?;
195/// # Ok(()) }
196/// ```
197pub fn delete(uri: impl AsRef<str>) -> RequestBuilder {
198    let uri = uri.as_ref().parse().unwrap();
199    RequestBuilder::new(Method::Delete, uri)
200}
201
202/// Perform a one-off `CONNECT` request.
203///
204/// # About the HTTP Method
205///
206/// The HTTP CONNECT method method starts two-way communications with the requested resource. It
207/// can be used to open a tunnel.
208///
209/// For example, the CONNECT method can be used to access websites that use SSL (HTTPS). The client
210/// asks an HTTP Proxy server to tunnel the TCP connection to the desired destination. The server
211/// then proceeds to make the connection on behalf of the client. Once the connection has been
212/// established by the server, the Proxy server continues to proxy the TCP stream to and from the
213/// client.
214///
215/// CONNECT is a hop-by-hop method.
216///
217/// [Read more on MDN]
218///
219/// [Read more on MDN]: https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/CONNECT
220///
221/// # Panics
222///
223/// This will panic if a malformed URL is passed.
224///
225/// # Errors
226///
227/// Returns errors from the middleware, http backend, and network sockets.
228///
229/// # Examples
230///
231/// ```no_run
232/// # #[async_std::main]
233/// # async fn main() -> surf::Result<()> {
234/// let string = surf::connect("https://httpbin.org/connect").recv_string().await?;
235/// # Ok(()) }
236/// ```
237pub fn connect(uri: impl AsRef<str>) -> RequestBuilder {
238    let uri = uri.as_ref().parse().unwrap();
239    RequestBuilder::new(Method::Connect, uri)
240}
241
242/// Perform a one-off `OPTIONS` request.
243///
244/// # About the HTTP Method
245///
246/// The HTTP OPTIONS method is used to describe the communication options for the target resource.
247/// The client can specify a URL for the OPTIONS method, or an asterisk (*) to refer to the entire
248/// server.
249///
250/// [Read more on MDN]
251///
252/// [Read more on MDN]: https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/OPTIONS
253///
254/// # Panics
255///
256/// This will panic if a malformed URL is passed.
257///
258/// # Errors
259///
260/// Returns errors from the middleware, http backend, and network sockets.
261///
262/// # Examples
263///
264/// ```no_run
265/// # #[async_std::main]
266/// # async fn main() -> surf::Result<()> {
267/// let string = surf::options("https://httpbin.org/options").recv_string().await?;
268/// # Ok(()) }
269/// ```
270pub fn options(uri: impl AsRef<str>) -> RequestBuilder {
271    let uri = uri.as_ref().parse().unwrap();
272    RequestBuilder::new(Method::Options, uri)
273}
274
275/// Perform a one-off `TRACE` request.
276///
277/// # About the HTTP Method
278///
279/// The HTTP TRACE method performs a message loop-back test along the path to the target resource,
280/// providing a useful debugging mechanism.
281///
282/// The final recipient of the request should reflect the message received, excluding some fields
283/// described below, back to the client as the message body of a 200 (OK) response with a
284/// Content-Type of message/http. The final recipient is either the origin server or the first
285/// server to receive a Max-Forwards value of 0 in the request.
286///
287/// [Read more on MDN]
288///
289/// [Read more on MDN]: https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/TRACE
290///
291/// # Panics
292///
293/// This will panic if a malformed URL is passed.
294///
295/// # Errors
296///
297/// Returns errors from the middleware, http backend, and network sockets.
298///
299/// # Examples
300///
301/// ```no_run
302/// # #[async_std::main]
303/// # async fn main() -> surf::Result<()> {
304/// let string = surf::trace("https://httpbin.org/trace").recv_string().await?;
305/// # Ok(()) }
306/// ```
307pub fn trace(uri: impl AsRef<str>) -> RequestBuilder {
308    let uri = uri.as_ref().parse().unwrap();
309    RequestBuilder::new(Method::Trace, uri)
310}
311
312/// Perform a one-off `PATCH` request.
313///
314/// # About the HTTP Method
315///
316/// The HTTP PATCH request method applies partial modifications to a resource.
317///
318/// The HTTP PUT method only allows complete replacement of a document. Unlike PUT, PATCH is not
319/// idempotent, meaning successive identical patch requests may have different effects. However, it
320/// is possible to issue PATCH requests in such a way as to be idempotent.
321///
322/// PATCH (like PUT) may have side-effects on other resources.
323///
324/// To find out whether a server supports PATCH, a server can advertise its support by adding it to
325/// the list in the Allow or Access-Control-Allow-Methods (for CORS) response headers.
326///
327/// Another (implicit) indication that PATCH is allowed, is the presence of the Accept-Patch
328/// header, which specifies the patch document formats accepted by the server.
329///
330/// [Read more on MDN]
331///
332/// [Read more on MDN]: https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/PATCH
333///
334/// # Panics
335///
336/// This will panic if a malformed URL is passed.
337///
338/// # Errors
339///
340/// Returns errors from the middleware, http backend, and network sockets.
341///
342/// # Examples
343///
344/// ```no_run
345/// # #[async_std::main]
346/// # async fn main() -> surf::Result<()> {
347/// let string = surf::patch("https://httpbin.org/patch").recv_string().await?;
348/// # Ok(()) }
349/// ```
350pub fn patch(uri: impl AsRef<str>) -> RequestBuilder {
351    let uri = uri.as_ref().parse().unwrap();
352    RequestBuilder::new(Method::Patch, uri)
353}