Files
2021-03-26 19:20:48 +00:00

164 lines
19 KiB
HTML
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<!DOCTYPE html><html lang="en"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><meta name="generator" content="rustdoc"><meta name="description" content="API documentation for the Rust `reqwest` crate."><meta name="keywords" content="rust, rustlang, rust-lang, reqwest"><title>reqwest - Rust</title><link rel="stylesheet" type="text/css" href="../normalize.css"><link rel="stylesheet" type="text/css" href="../rustdoc.css" id="mainThemeStyle"><link rel="stylesheet" type="text/css" href="../light.css" id="themeStyle"><link rel="stylesheet" type="text/css" href="../dark.css" disabled ><link rel="stylesheet" type="text/css" href="../ayu.css" disabled ><script id="default-settings"></script><script src="../storage.js"></script><noscript><link rel="stylesheet" href="../noscript.css"></noscript><link rel="icon" type="image/svg+xml" href="../favicon.svg">
<link rel="alternate icon" type="image/png" href="../favicon-16x16.png">
<link rel="alternate icon" type="image/png" href="../favicon-32x32.png"><style type="text/css">#crate-search{background-image:url("../down-arrow.svg");}</style></head><body class="rustdoc mod"><!--[if lte IE 8]><div class="warning">This old browser is unsupported and will most likely display funky things.</div><![endif]--><nav class="sidebar"><div class="sidebar-menu">&#9776;</div><a href='../reqwest/index.html'><div class='logo-container rust-logo'><img src='../rust-logo.png' alt='logo'></div></a><p class="location">Crate reqwest</p><div class="block version"><p>Version 0.11.2</p></div><div class="sidebar-elems"><a id="all-types" href="all.html"><p>See all reqwest's items</p></a><div class="block items"><ul><li><a href="#modules">Modules</a></li><li><a href="#structs">Structs</a></li><li><a href="#traits">Traits</a></li><li><a href="#functions">Functions</a></li><li><a href="#types">Type Definitions</a></li></ul></div><p class="location"></p><div id="sidebar-vars" data-name="reqwest" data-ty="mod" data-relpath="../"></div></div></nav><div class="theme-picker"><button id="theme-picker" aria-label="Pick another theme!" aria-haspopup="menu"><img src="../brush.svg" width="18" alt="Pick another theme!"></button><div id="theme-choices" role="menu"></div></div><script src="../theme.js"></script><nav class="sub"><form class="search-form"><div class="search-container"><div><select id="crate-search"><option value="All crates">All crates</option></select><input class="search-input" name="search" disabled autocomplete="off" spellcheck="false" placeholder="Click or press S to search, ? for more options…" type="search"></div><button type="button" class="help-button">?</button>
<a id="settings-menu" href="../settings.html"><img src="../wheel.svg" width="18" alt="Change settings"></a></div></form></nav><section id="main" class="content"><h1 class="fqn"><span class="in-band">Crate <a class="mod" href="">reqwest</a></span><span class="out-of-band"><span id="render-detail"><a id="toggle-all-docs" href="javascript:void(0)" title="collapse all docs">[<span class="inner">&#x2212;</span>]</a></span><a class="srclink" href="../src/reqwest/lib.rs.html#1-326" title="goto source code">[src]</a></span></h1><div class="docblock"><h1 id="reqwest" class="section-header"><a href="#reqwest">reqwest</a></h1>
<p>The <code>reqwest</code> crate provides a convenient, higher-level HTTP
<a href="./struct.Client.html"><code>Client</code></a>.</p>
<p>It handles many of the things that most people just expect an HTTP client
to do for them.</p>
<ul>
<li>Async and <a href="blocking">blocking</a> Clients</li>
<li>Plain bodies, <a href="#json">JSON</a>, <a href="#forms">urlencoded</a>, <a href="multipart">multipart</a></li>
<li>Customizable <a href="#redirect-policies">redirect policy</a></li>
<li>HTTP <a href="#proxies">Proxies</a></li>
<li>Uses system-native <a href="#tls">TLS</a></li>
<li>Cookies</li>
</ul>
<p>The <a href="./struct.Client.html"><code>reqwest::Client</code></a> is asynchronous. For applications wishing
to only make a few HTTP requests, the <a href="blocking"><code>reqwest::blocking</code></a> API
may be more convenient.</p>
<p>Additional learning resources include:</p>
<ul>
<li><a href="https://rust-lang-nursery.github.io/rust-cookbook/web/clients.html">The Rust Cookbook</a></li>
<li><a href="https://github.com/seanmonstar/reqwest/tree/master/examples">Reqwest Repository Examples</a></li>
</ul>
<h2 id="making-a-get-request" class="section-header"><a href="#making-a-get-request">Making a GET request</a></h2>
<p>For a single request, you can use the <a href="./fn.get.html"><code>get</code></a> shortcut method.</p>
<div class="example-wrap"><pre class="rust rust-example-rendered">
<span class="kw">let</span> <span class="ident">body</span> <span class="op">=</span> <span class="ident">reqwest</span>::<span class="ident">get</span>(<span class="string">&quot;https://www.rust-lang.org&quot;</span>)
.<span class="kw">await</span><span class="question-mark">?</span>
.<span class="ident">text</span>()
.<span class="kw">await</span><span class="question-mark">?</span>;
<span class="macro">println</span><span class="macro">!</span>(<span class="string">&quot;body = {:?}&quot;</span>, <span class="ident">body</span>);</pre></div>
<p><strong>NOTE</strong>: If you plan to perform multiple requests, it is best to create a
<a href="./struct.Client.html"><code>Client</code></a> and reuse it, taking advantage of keep-alive connection
pooling.</p>
<h2 id="making-post-requests-or-setting-request-bodies" class="section-header"><a href="#making-post-requests-or-setting-request-bodies">Making POST requests (or setting request bodies)</a></h2>
<p>There are several ways you can set the body of a request. The basic one is
by using the <code>body()</code> method of a <a href="./struct.RequestBuilder.html"><code>RequestBuilder</code></a>. This lets you set the
exact raw bytes of what the body should be. It accepts various types,
including <code>String</code>, <code>Vec&lt;u8&gt;</code>, and <code>File</code>. If you wish to pass a custom
type, you can use the <code>reqwest::Body</code> constructors.</p>
<div class="example-wrap"><pre class="rust rust-example-rendered">
<span class="kw">let</span> <span class="ident">client</span> <span class="op">=</span> <span class="ident">reqwest</span>::<span class="ident">Client</span>::<span class="ident">new</span>();
<span class="kw">let</span> <span class="ident">res</span> <span class="op">=</span> <span class="ident">client</span>.<span class="ident">post</span>(<span class="string">&quot;http://httpbin.org/post&quot;</span>)
.<span class="ident">body</span>(<span class="string">&quot;the exact body that is sent&quot;</span>)
.<span class="ident">send</span>()
.<span class="kw">await</span><span class="question-mark">?</span>;</pre></div>
<h3 id="forms" class="section-header"><a href="#forms">Forms</a></h3>
<p>It's very common to want to send form data in a request body. This can be
done with any type that can be serialized into form data.</p>
<p>This can be an array of tuples, or a <code>HashMap</code>, or a custom type that
implements <a href="http://serde.rs"><code>Serialize</code></a>.</p>
<div class="example-wrap"><pre class="rust rust-example-rendered">
<span class="comment">// This will POST a body of `foo=bar&amp;baz=quux`</span>
<span class="kw">let</span> <span class="ident">params</span> <span class="op">=</span> [(<span class="string">&quot;foo&quot;</span>, <span class="string">&quot;bar&quot;</span>), (<span class="string">&quot;baz&quot;</span>, <span class="string">&quot;quux&quot;</span>)];
<span class="kw">let</span> <span class="ident">client</span> <span class="op">=</span> <span class="ident">reqwest</span>::<span class="ident">Client</span>::<span class="ident">new</span>();
<span class="kw">let</span> <span class="ident">res</span> <span class="op">=</span> <span class="ident">client</span>.<span class="ident">post</span>(<span class="string">&quot;http://httpbin.org/post&quot;</span>)
.<span class="ident">form</span>(<span class="kw-2">&amp;</span><span class="ident">params</span>)
.<span class="ident">send</span>()
.<span class="kw">await</span><span class="question-mark">?</span>;</pre></div>
<h3 id="json" class="section-header"><a href="#json">JSON</a></h3>
<p>There is also a <code>json</code> method helper on the <a href="./struct.RequestBuilder.html"><code>RequestBuilder</code></a> that works in
a similar fashion the <code>form</code> method. It can take any value that can be
serialized into JSON. The feature <code>json</code> is required.</p>
<div class="example-wrap"><pre class="rust rust-example-rendered">
<span class="comment">// This will POST a body of `{&quot;lang&quot;:&quot;rust&quot;,&quot;body&quot;:&quot;json&quot;}`</span>
<span class="kw">let</span> <span class="kw-2">mut</span> <span class="ident">map</span> <span class="op">=</span> <span class="ident">HashMap</span>::<span class="ident">new</span>();
<span class="ident">map</span>.<span class="ident">insert</span>(<span class="string">&quot;lang&quot;</span>, <span class="string">&quot;rust&quot;</span>);
<span class="ident">map</span>.<span class="ident">insert</span>(<span class="string">&quot;body&quot;</span>, <span class="string">&quot;json&quot;</span>);
<span class="kw">let</span> <span class="ident">client</span> <span class="op">=</span> <span class="ident">reqwest</span>::<span class="ident">Client</span>::<span class="ident">new</span>();
<span class="kw">let</span> <span class="ident">res</span> <span class="op">=</span> <span class="ident">client</span>.<span class="ident">post</span>(<span class="string">&quot;http://httpbin.org/post&quot;</span>)
.<span class="ident">json</span>(<span class="kw-2">&amp;</span><span class="ident">map</span>)
.<span class="ident">send</span>()
.<span class="kw">await</span><span class="question-mark">?</span>;</pre></div>
<h2 id="redirect-policies" class="section-header"><a href="#redirect-policies">Redirect Policies</a></h2>
<p>By default, a <code>Client</code> will automatically handle HTTP redirects, having a
maximum redirect chain of 10 hops. To customize this behavior, a
<a href="../reqwest/redirect/index.html"><code>redirect::Policy</code></a> can be used with a <code>ClientBuilder</code>.</p>
<h2 id="cookies" class="section-header"><a href="#cookies">Cookies</a></h2>
<p>The automatic storing and sending of session cookies can be enabled with
the [<code>cookie_store</code>][ClientBuilder::cookie_store] method on <code>ClientBuilder</code>.</p>
<h2 id="proxies" class="section-header"><a href="#proxies">Proxies</a></h2>
<p><strong>NOTE</strong>: System proxies are enabled by default.</p>
<p>System proxies look in environment variables to set HTTP or HTTPS proxies.</p>
<p><code>HTTP_PROXY</code> or <code>http_proxy</code> provide http proxies for http connections while
<code>HTTPS_PROXY</code> or <code>https_proxy</code> provide HTTPS proxies for HTTPS connections.</p>
<p>These can be overwritten by adding a <a href="../reqwest/struct.Proxy.html"><code>Proxy</code></a> to <code>ClientBuilder</code>
i.e. <code>let proxy = reqwest::Proxy::http(&quot;https://secure.example&quot;)?;</code>
or disabled by calling <code>ClientBuilder::no_proxy()</code>.</p>
<p><code>socks</code> feature is required if you have configured socks proxy like this:</p>
<pre><code class="language-bash">export https_proxy=socks5://127.0.0.1:1086
</code></pre>
<h2 id="tls" class="section-header"><a href="#tls">TLS</a></h2>
<p>By default, a <code>Client</code> will make use of system-native transport layer
security to connect to HTTPS destinations. This means schannel on Windows,
Security-Framework on macOS, and OpenSSL on Linux.</p>
<ul>
<li>Additional X509 certificates can be configured on a <code>ClientBuilder</code> with the
<a href="../reqwest/struct.Certificate.html"><code>Certificate</code></a> type.</li>
<li>Client certificates can be add to a <code>ClientBuilder</code> with the
<a href="../reqwest/struct.Identity.html" title="Identity"><code>Identity</code></a> type.</li>
<li>Various parts of TLS can also be configured or even disabled on the
<code>ClientBuilder</code>.</li>
</ul>
<h2 id="optional-features" class="section-header"><a href="#optional-features">Optional Features</a></h2>
<p>The following are a list of <a href="https://doc.rust-lang.org/stable/cargo/reference/manifest.html#the-features-section">Cargo features</a> that can be
enabled or disabled:</p>
<ul>
<li><strong>default-tls</strong> <em>(enabled by default)</em>: Provides TLS support to connect
over HTTPS.</li>
<li><strong>native-tls</strong>: Enables TLS functionality provided by <code>native-tls</code>.</li>
<li><strong>native-tls-vendored</strong>: Enables the <code>vendored</code> feature of <code>native-tls</code>.</li>
<li><strong>rustls-tls</strong>: Enables TLS functionality provided by <code>rustls</code>.
Equivalent to <code>rustls-tls-webpki-roots</code>.</li>
<li><strong>rustls-tls-manual-roots</strong>: Enables TLS functionality provided by <code>rustls</code>,
without setting any root certificates. Roots have to be specified manually.</li>
<li><strong>rustls-tls-webpki-roots</strong>: Enables TLS functionality provided by <code>rustls</code>,
while using root certificates from the <code>webpki-roots</code> crate.</li>
<li><strong>rustls-tls-native-roots</strong>: Enables TLS functionality provided by <code>rustls</code>,
while using root certificates from the <code>rustls-native-certs</code> crate.</li>
<li><strong>blocking</strong>: Provides the <a href="./blocking/index.html">blocking</a> client API.</li>
<li><strong>cookies</strong>: Provides cookie session support.</li>
<li><strong>gzip</strong>: Provides response body gzip decompression.</li>
<li><strong>brotli</strong>: Provides response body brotli decompression.</li>
<li><strong>json</strong>: Provides serialization and deserialization for JSON bodies.</li>
<li><strong>multipart</strong>: Provides functionality for multipart forms.</li>
<li><strong>stream</strong>: Adds support for <code>futures::Stream</code>.</li>
<li><strong>socks</strong>: Provides SOCKS5 proxy support.</li>
<li><strong>trust-dns</strong>: Enables a trust-dns async resolver instead of default
threadpool using <code>getaddrinfo</code>.</li>
</ul>
</div><h2 id="modules" class="section-header"><a href="#modules">Modules</a></h2>
<table><tr class="module-item"><td><a class="mod" href="header/index.html" title="reqwest::header mod">header</a></td><td class="docblock-short"><p>HTTP header types</p>
</td></tr><tr class="module-item"><td><a class="mod" href="redirect/index.html" title="reqwest::redirect mod">redirect</a></td><td class="docblock-short"><p>Redirect Handling</p>
</td></tr></table><h2 id="structs" class="section-header"><a href="#structs">Structs</a></h2>
<table><tr class="module-item"><td><a class="struct" href="struct.Body.html" title="reqwest::Body struct">Body</a></td><td class="docblock-short"><p>An asynchronous request body.</p>
</td></tr><tr class="module-item"><td><a class="struct" href="struct.Certificate.html" title="reqwest::Certificate struct">Certificate</a></td><td class="docblock-short"><p>Represents a server X509 certificate.</p>
</td></tr><tr class="module-item"><td><a class="struct" href="struct.Client.html" title="reqwest::Client struct">Client</a></td><td class="docblock-short"><p>An asynchronous <code>Client</code> to make Requests with.</p>
</td></tr><tr class="module-item"><td><a class="struct" href="struct.ClientBuilder.html" title="reqwest::ClientBuilder struct">ClientBuilder</a></td><td class="docblock-short"><p>A <code>ClientBuilder</code> can be used to create a <code>Client</code> with custom configuration.</p>
</td></tr><tr class="module-item"><td><a class="struct" href="struct.Error.html" title="reqwest::Error struct">Error</a></td><td class="docblock-short"><p>The Errors that may occur when processing a <code>Request</code>.</p>
</td></tr><tr class="module-item"><td><a class="struct" href="struct.Identity.html" title="reqwest::Identity struct">Identity</a></td><td class="docblock-short"><p>Represents a private key and X509 cert as a client certificate.</p>
</td></tr><tr class="module-item"><td><a class="struct" href="struct.Method.html" title="reqwest::Method struct">Method</a></td><td class="docblock-short"><p>The Request Method (VERB)</p>
</td></tr><tr class="module-item"><td><a class="struct" href="struct.Proxy.html" title="reqwest::Proxy struct">Proxy</a></td><td class="docblock-short"><p>Configuration of a proxy that a <code>Client</code> should pass requests to.</p>
</td></tr><tr class="module-item"><td><a class="struct" href="struct.Request.html" title="reqwest::Request struct">Request</a></td><td class="docblock-short"><p>A request which can be executed with <code>Client::execute()</code>.</p>
</td></tr><tr class="module-item"><td><a class="struct" href="struct.RequestBuilder.html" title="reqwest::RequestBuilder struct">RequestBuilder</a></td><td class="docblock-short"><p>A builder to construct the properties of a <code>Request</code>.</p>
</td></tr><tr class="module-item"><td><a class="struct" href="struct.Response.html" title="reqwest::Response struct">Response</a></td><td class="docblock-short"><p>A Response to a submitted <code>Request</code>.</p>
</td></tr><tr class="module-item"><td><a class="struct" href="struct.StatusCode.html" title="reqwest::StatusCode struct">StatusCode</a></td><td class="docblock-short"><p>An HTTP status code (<code>status-code</code> in RFC 7230 et al.).</p>
</td></tr><tr class="module-item"><td><a class="struct" href="struct.Url.html" title="reqwest::Url struct">Url</a></td><td class="docblock-short"><p>A parsed URL record.</p>
</td></tr><tr class="module-item"><td><a class="struct" href="struct.Version.html" title="reqwest::Version struct">Version</a></td><td class="docblock-short"><p>Represents a version of the HTTP spec.</p>
</td></tr></table><h2 id="traits" class="section-header"><a href="#traits">Traits</a></h2>
<table><tr class="module-item"><td><a class="trait" href="trait.IntoUrl.html" title="reqwest::IntoUrl trait">IntoUrl</a></td><td class="docblock-short"><p>A trait to try to convert some type into a <code>Url</code>.</p>
</td></tr><tr class="module-item"><td><a class="trait" href="trait.ResponseBuilderExt.html" title="reqwest::ResponseBuilderExt trait">ResponseBuilderExt</a></td><td class="docblock-short"><p>Extension trait for http::response::Builder objects</p>
</td></tr></table><h2 id="functions" class="section-header"><a href="#functions">Functions</a></h2>
<table><tr class="module-item"><td><a class="fn" href="fn.get.html" title="reqwest::get fn">get</a></td><td class="docblock-short"><p>Shortcut method to quickly make a <code>GET</code> request.</p>
</td></tr></table><h2 id="types" class="section-header"><a href="#types">Type Definitions</a></h2>
<table><tr class="module-item"><td><a class="type" href="type.Result.html" title="reqwest::Result type">Result</a></td><td class="docblock-short"><p>A <code>Result</code> alias where the <code>Err</code> case is <code>reqwest::Error</code>.</p>
</td></tr></table></section><section id="search" class="content hidden"></section><section class="footer"></section><div id="rustdoc-vars" data-root-path="../" data-current-crate="reqwest"></div>
<script src="../main.js"></script><script defer src="../search-index.js"></script></body></html>