blob: c4ea95dd94df8d457fe20e9c38d73d18badc0293 [file] [log] [blame]
<!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="reqwest"><meta name="keywords" content="rust, rustlang, rust-lang, reqwest"><title>reqwest - Rust</title><link rel="preload" as="font" type="font/woff2" crossorigin href="../SourceSerif4-Regular.ttf.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../FiraSans-Regular.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../FiraSans-Medium.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../SourceCodePro-Regular.ttf.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../SourceSerif4-Bold.ttf.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../SourceCodePro-Semibold.ttf.woff2"><link rel="stylesheet" href="../normalize.css"><link rel="stylesheet" href="../rustdoc.css" id="mainThemeStyle"><link rel="stylesheet" href="../ayu.css" disabled><link rel="stylesheet" href="../dark.css" disabled><link rel="stylesheet" href="../light.css" id="themeStyle"><script id="default-settings" ></script><script src="../storage.js"></script><script defer src="../crates.js"></script><script defer src="../main.js"></script><noscript><link rel="stylesheet" href="../noscript.css"></noscript><link rel="alternate icon" type="image/png" href="../favicon-16x16.png"><link rel="alternate icon" type="image/png" href="../favicon-32x32.png"><link rel="icon" type="image/svg+xml" href="../favicon.svg"></head><body class="rustdoc mod crate"><!--[if lte IE 11]><div class="warning">This old browser is unsupported and will most likely display funky things.</div><![endif]--><nav class="mobile-topbar"><button class="sidebar-menu-toggle">&#9776;</button><a class="sidebar-logo" href="../reqwest/index.html"><div class="logo-container"><img class="rust-logo" src="../rust-logo.svg" alt="logo"></div></a><h2></h2></nav><nav class="sidebar"><a class="sidebar-logo" href="../reqwest/index.html"><div class="logo-container"><img class="rust-logo" src="../rust-logo.svg" alt="logo"></div></a><h2 class="location"><a href="#">Crate reqwest</a></h2><div class="sidebar-elems"><ul class="block"><li class="version">Version 0.11.18</li><li><a id="all-types" href="all.html">All Items</a></li></ul><section><ul class="block"><li><a href="#reexports">Re-exports</a></li><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></section></div></nav><main><div class="width-limiter"><nav class="sub"><form class="search-form"><div class="search-container"><span></span><input class="search-input" name="search" autocomplete="off" spellcheck="false" placeholder="Click or press ‘S’ to search, ‘?’ for more options…" type="search"><div id="help-button" title="help" tabindex="-1"><a href="../help.html">?</a></div><div id="settings-menu" tabindex="-1"><a href="../settings.html" title="settings"><img width="22" height="22" alt="Change settings" src="../wheel.svg"></a></div></div></form></nav><section id="main-content" class="content"><div class="main-heading"><h1 class="fqn">Crate <a class="mod" href="#">reqwest</a><button id="copy-path" onclick="copy_path(this)" title="Copy item path to clipboard"><img src="../clipboard.svg" width="19" height="18" alt="Copy item path"></button></h1><span class="out-of-band"><a class="srclink" href="../src/reqwest/lib.rs.html#1-351">source</a> · <a id="toggle-all-docs" href="javascript:void(0)" title="collapse all docs">[<span class="inner">&#x2212;</span>]</a></span></div><details class="rustdoc-toggle top-doc" open><summary class="hideme"><span>Expand description</span></summary><div class="docblock"><h2 id="reqwest"><a href="#reqwest">reqwest</a></h2>
<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>
<h3 id="making-a-get-request"><a href="#making-a-get-request">Making a GET request</a></h3>
<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"><code><span class="kw">let </span>body = reqwest::get(<span class="string">&quot;https://www.rust-lang.org&quot;</span>)
.<span class="kw">await</span><span class="question-mark">?
</span>.text()
.<span class="kw">await</span><span class="question-mark">?</span>;
<span class="macro">println!</span>(<span class="string">&quot;body = {:?}&quot;</span>, body);</code></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>
<h3 id="making-post-requests-or-setting-request-bodies"><a href="#making-post-requests-or-setting-request-bodies">Making POST requests (or setting request bodies)</a></h3>
<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> and <code>Vec&lt;u8&gt;</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"><code><span class="kw">let </span>client = reqwest::Client::new();
<span class="kw">let </span>res = client.post(<span class="string">&quot;http://httpbin.org/post&quot;</span>)
.body(<span class="string">&quot;the exact body that is sent&quot;</span>)
.send()
.<span class="kw">await</span><span class="question-mark">?</span>;</code></pre></div>
<h4 id="forms"><a href="#forms">Forms</a></h4>
<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"><code><span class="comment">// This will POST a body of `foo=bar&amp;baz=quux`
</span><span class="kw">let </span>params = [(<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>client = reqwest::Client::new();
<span class="kw">let </span>res = client.post(<span class="string">&quot;http://httpbin.org/post&quot;</span>)
.form(<span class="kw-2">&amp;</span>params)
.send()
.<span class="kw">await</span><span class="question-mark">?</span>;</code></pre></div>
<h4 id="json"><a href="#json">JSON</a></h4>
<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"><code><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>map = HashMap::new();
map.insert(<span class="string">&quot;lang&quot;</span>, <span class="string">&quot;rust&quot;</span>);
map.insert(<span class="string">&quot;body&quot;</span>, <span class="string">&quot;json&quot;</span>);
<span class="kw">let </span>client = reqwest::Client::new();
<span class="kw">let </span>res = client.post(<span class="string">&quot;http://httpbin.org/post&quot;</span>)
.json(<span class="kw-2">&amp;</span>map)
.send()
.<span class="kw">await</span><span class="question-mark">?</span>;</code></pre></div>
<h3 id="redirect-policies"><a href="#redirect-policies">Redirect Policies</a></h3>
<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="redirect/index.html"><code>redirect::Policy</code></a> can be used with a <code>ClientBuilder</code>.</p>
<h3 id="cookies"><a href="#cookies">Cookies</a></h3>
<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>
<h3 id="proxies"><a href="#proxies">Proxies</a></h3>
<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="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>
<div class="example-wrap"><pre class="language-bash"><code>export https_proxy=socks5://127.0.0.1:1086</code></pre></div><h3 id="tls"><a href="#tls">TLS</a></h3>
<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="tls/struct.Certificate.html"><code>Certificate</code></a> type.</li>
<li>Client certificates can be added to a <code>ClientBuilder</code> with the
<a href="tls/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>
<h3 id="optional-features"><a href="#optional-features">Optional Features</a></h3>
<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>native-tls-alpn</strong>: Enables the <code>alpn</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>deflate</strong>: Provides response body deflate 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>
<h3 id="unstable-features"><a href="#unstable-features">Unstable Features</a></h3>
<p>Some feature flags require additional opt-in by the application, by setting
a <code>reqwest_unstable</code> flag.</p>
<ul>
<li><strong>http3</strong> <em>(unstable)</em>: Enables support for sending HTTP/3 requests.</li>
</ul>
<p>These features are unstable, and experimental. Details about them may be
changed in patch releases.</p>
<p>You can pass such a flag to the compiler via <code>.cargo/config</code>, or
environment variables, such as:</p>
<div class="example-wrap"><pre class="language-notrust"><code>RUSTFLAGS=&quot;--cfg reqwest_unstable&quot; cargo build</code></pre></div></div></details><h2 id="reexports" class="small-section-header"><a href="#reexports">Re-exports</a></h2><div class="item-table"><div class="item-row"><div class="item-left import-item" id="reexport.Certificate"><code>pub use tls::<a class="struct" href="tls/struct.Certificate.html" title="struct reqwest::tls::Certificate">Certificate</a>;</code></div></div><div class="item-row"><div class="item-left import-item" id="reexport.Identity"><code>pub use tls::<a class="struct" href="tls/struct.Identity.html" title="struct reqwest::tls::Identity">Identity</a>;</code></div></div></div><h2 id="modules" class="small-section-header"><a href="#modules">Modules</a></h2><div class="item-table"><div class="item-row"><div class="item-left module-item"><a class="mod" href="dns/index.html" title="reqwest::dns mod">dns</a></div><div class="item-right docblock-short">DNS resolution</div></div><div class="item-row"><div class="item-left module-item"><a class="mod" href="header/index.html" title="reqwest::header mod">header</a></div><div class="item-right docblock-short">HTTP header types</div></div><div class="item-row"><div class="item-left module-item"><a class="mod" href="redirect/index.html" title="reqwest::redirect mod">redirect</a></div><div class="item-right docblock-short">Redirect Handling</div></div><div class="item-row"><div class="item-left module-item"><a class="mod" href="tls/index.html" title="reqwest::tls mod">tls</a></div><div class="item-right docblock-short">TLS configuration</div></div></div><h2 id="structs" class="small-section-header"><a href="#structs">Structs</a></h2><div class="item-table"><div class="item-row"><div class="item-left module-item"><a class="struct" href="struct.Body.html" title="reqwest::Body struct">Body</a></div><div class="item-right docblock-short">An asynchronous request body.</div></div><div class="item-row"><div class="item-left module-item"><a class="struct" href="struct.Client.html" title="reqwest::Client struct">Client</a></div><div class="item-right docblock-short">An asynchronous <code>Client</code> to make Requests with.</div></div><div class="item-row"><div class="item-left module-item"><a class="struct" href="struct.ClientBuilder.html" title="reqwest::ClientBuilder struct">ClientBuilder</a></div><div class="item-right docblock-short">A <code>ClientBuilder</code> can be used to create a <code>Client</code> with custom configuration.</div></div><div class="item-row"><div class="item-left module-item"><a class="struct" href="struct.Error.html" title="reqwest::Error struct">Error</a></div><div class="item-right docblock-short">The Errors that may occur when processing a <code>Request</code>.</div></div><div class="item-row"><div class="item-left module-item"><a class="struct" href="struct.Method.html" title="reqwest::Method struct">Method</a></div><div class="item-right docblock-short">The Request Method (VERB)</div></div><div class="item-row"><div class="item-left module-item"><a class="struct" href="struct.NoProxy.html" title="reqwest::NoProxy struct">NoProxy</a></div><div class="item-right docblock-short">A configuration for filtering out requests that shouldn’t be proxied</div></div><div class="item-row"><div class="item-left module-item"><a class="struct" href="struct.Proxy.html" title="reqwest::Proxy struct">Proxy</a></div><div class="item-right docblock-short">Configuration of a proxy that a <code>Client</code> should pass requests to.</div></div><div class="item-row"><div class="item-left module-item"><a class="struct" href="struct.Request.html" title="reqwest::Request struct">Request</a></div><div class="item-right docblock-short">A request which can be executed with <code>Client::execute()</code>.</div></div><div class="item-row"><div class="item-left module-item"><a class="struct" href="struct.RequestBuilder.html" title="reqwest::RequestBuilder struct">RequestBuilder</a></div><div class="item-right docblock-short">A builder to construct the properties of a <code>Request</code>.</div></div><div class="item-row"><div class="item-left module-item"><a class="struct" href="struct.Response.html" title="reqwest::Response struct">Response</a></div><div class="item-right docblock-short">A Response to a submitted <code>Request</code>.</div></div><div class="item-row"><div class="item-left module-item"><a class="struct" href="struct.StatusCode.html" title="reqwest::StatusCode struct">StatusCode</a></div><div class="item-right docblock-short">An HTTP status code (<code>status-code</code> in RFC 7230 et al.).</div></div><div class="item-row"><div class="item-left module-item"><a class="struct" href="struct.Upgraded.html" title="reqwest::Upgraded struct">Upgraded</a></div><div class="item-right docblock-short">An upgraded HTTP connection.</div></div><div class="item-row"><div class="item-left module-item"><a class="struct" href="struct.Url.html" title="reqwest::Url struct">Url</a></div><div class="item-right docblock-short">A parsed URL record.</div></div><div class="item-row"><div class="item-left module-item"><a class="struct" href="struct.Version.html" title="reqwest::Version struct">Version</a></div><div class="item-right docblock-short">Represents a version of the HTTP spec.</div></div></div><h2 id="traits" class="small-section-header"><a href="#traits">Traits</a></h2><div class="item-table"><div class="item-row"><div class="item-left module-item"><a class="trait" href="trait.IntoUrl.html" title="reqwest::IntoUrl trait">IntoUrl</a></div><div class="item-right docblock-short">A trait to try to convert some type into a <code>Url</code>.</div></div><div class="item-row"><div class="item-left module-item"><a class="trait" href="trait.ResponseBuilderExt.html" title="reqwest::ResponseBuilderExt trait">ResponseBuilderExt</a></div><div class="item-right docblock-short">Extension trait for http::response::Builder objects</div></div></div><h2 id="functions" class="small-section-header"><a href="#functions">Functions</a></h2><div class="item-table"><div class="item-row"><div class="item-left module-item"><a class="fn" href="fn.get.html" title="reqwest::get fn">get</a></div><div class="item-right docblock-short">Shortcut method to quickly make a <code>GET</code> request.</div></div></div><h2 id="types" class="small-section-header"><a href="#types">Type Definitions</a></h2><div class="item-table"><div class="item-row"><div class="item-left module-item"><a class="type" href="type.Result.html" title="reqwest::Result type">Result</a></div><div class="item-right docblock-short">A <code>Result</code> alias where the <code>Err</code> case is <code>reqwest::Error</code>.</div></div></div></section></div></main><div id="rustdoc-vars" data-root-path="../" data-current-crate="reqwest" data-themes="ayu,dark,light" data-resource-suffix="" data-rustdoc-version="1.66.0-nightly (5c8bff74b 2022-10-21)" ></div></body></html>