<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<link rel="icon" href="https://avatars0.githubusercontent.com/u/58360786?s=200&v=4" title="Kayrx">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/katex@0.10.2/dist/katex.min.css" integrity="sha384-yFRtMMDnQtDRO8rLpMIKrtPCD5jdktao2TV19YiZYWMDkUR5GQZR/NOVTdquEx1j" crossorigin="anonymous">
<link rel="stylesheet" type="text/css" href="https://cdn.jsdelivr.net/npm/katex-copytex@latest/dist/katex-copytex.min.css">
<script src="https://cdn.jsdelivr.net/npm/katex-copytex@latest/dist/katex-copytex.min.js"></script>
<link rel="stylesheet" type="text/css" href="https://cdn.jsdelivr.net/gh/Microsoft/vscode/extensions/markdown-language-features/media/markdown.css">
<link rel="stylesheet" type="text/css" href="https://cdn.jsdelivr.net/gh/Microsoft/vscode/extensions/markdown-language-features/media/highlight.css">
<link rel="stylesheet" type="text/css" href="https://kayrx.xyz/home.css" />
<title>Kayrx / handler</title>
</head>
<body class="vscode-light">
<div id="page" class="page-section-doc">
<div id="mnav">
<div id="lnav">
<div id="left">
<a id="front" href="/"><img src="/kayrx.png"/></a>
<span id="dlnav">
<a class="tail" href="/kayrx/" >Kayrx</a>
<a class="tail" href="/yarx" >Yarx</a>
<a class="tail" href="/blog" >Blog</a>
</span>
</div>
<label ><a href="#" id="menu">M</a></label>
</div>
<div id="rnav">
<li > <a href="https://spectrum.chat/kayrx" target="_black" style="color:green; font-weight: bold;" >群聊</a></li>
<li ><a href="/community" >社区</a></li>
<li > <a href="/funding">赞助</a></li>
</div>
</div>
<div id="content">
<div class="container"><div class="doc-page doc-section-basic-handler">
<div class="doc-nav">
<details id="pc" open>
<summary id="dir"><strong>目录指南</strong></summary>
<details open>
<summary id="title"><strong>介绍</strong></summary>
<a class="doc-section-link-introduction-index" href="/kayrx/introduction/index.html">欢迎</a>
<a class="doc-section-link-introduction-whatis" href="/kayrx/introduction/whatis.html">何为Actix</a>
<a class="doc-section-link-introduction-install" href="/kayrx/introduction/install.html">安装</a>
</details>
<details open>
<summary id="title"><strong>基本</strong></summary>
<a class="doc-section-link-basic-start" href="/kayrx/basic/start.html">入门</a>
<a class="doc-section-link-basic-app" href="/kayrx/basic/app.html">应用</a>
<a class="doc-section-link-basic-server" href="/kayrx/basic/server.html">服务器</a>
<a class="doc-section-link-basic-handler" href="/kayrx/basic/handler.html">处理器</a>
<a class="doc-section-link-basic-extractor" href="/kayrx/basic/extractor.html">提取器</a>
</details>
<details open>
<summary id="title"><strong>高级</strong></summary>
<a class="doc-section-link-advance-error" href="/kayrx/advance/error.html">错误</a>
<a class="doc-section-link-advance-url-dispatch" href="/kayrx/advance/url-dispatch.html">路由分发</a>
<a class="doc-section-link-advance-request" href="/kayrx/advance/request.html">请求</a>
<a class="doc-section-link-advance-response" href="/kayrx/advance/response.html">响应</a>
<a class="doc-section-link-advance-test" href="/kayrx/advance/test.html">测试</a>
<a class="doc-section-link-advance-middleware" href="/kayrx/advance/middleware.html">中间件</a>
<a class="doc-section-link-advance-static-file" href="/kayrx/advance/static-file.html">静态文件</a>
</details>
<details open>
<summary id="title"><strong>协议</strong></summary>
<a class="doc-section-link-protocol-websocket" href="/kayrx/protocol/websocket.html">Websocket</a>
<a class="doc-section-link-protocol-http2" href="/kayrx/protocol/http2.html">HTTP2</a>
</details>
<details open>
<summary id="title"><strong>模式</strong></summary>
<a class="doc-section-link-pattern-autoreload" href="/kayrx/pattern/autoreload.html">自动重载</a>
<a class="doc-section-link-pattern-database" href="/kayrx/pattern/database.html">数据库</a>
</details>
</details>
<details id="mobile">
<summary id="dir"><strong>目录指南</strong></summary>
<details open>
<summary id="title"><strong>介绍</strong></summary>
<a class="doc-section-link-introduction-index" href="/kayrx/introduction/index.html">欢迎</a>
<a class="doc-section-link-introduction-whatis" href="/kayrx/introduction/whatis.html">何为Actix</a>
<a class="doc-section-link-introduction-install" href="/kayrx/introduction/install.html">安装</a>
</details>
<details open>
<summary id="title"><strong>基本</strong></summary>
<a class="doc-section-link-basic-start" href="/kayrx/basic/start.html">入门</a>
<a class="doc-section-link-basic-app" href="/kayrx/basic/app.html">应用</a>
<a class="doc-section-link-basic-server" href="/kayrx/basic/server.html">服务器</a>
<a class="doc-section-link-basic-handler" href="/kayrx/basic/handler.html">处理器</a>
<a class="doc-section-link-basic-extractor" href="/kayrx/basic/extractor.html">提取器</a>
</details>
<details open>
<summary id="title"><strong>高级</strong></summary>
<a class="doc-section-link-advance-error" href="/kayrx/advance/error.html">错误</a>
<a class="doc-section-link-advance-url-dispatch" href="/kayrx/advance/url-dispatch.html">路由分发</a>
<a class="doc-section-link-advance-request" href="/kayrx/advance/request.html">请求</a>
<a class="doc-section-link-advance-response" href="/kayrx/advance/response.html">响应</a>
<a class="doc-section-link-advance-test" href="/kayrx/advance/test.html">测试</a>
<a class="doc-section-link-advance-middleware" href="/kayrx/advance/middleware.html">中间件</a>
<a class="doc-section-link-advance-static-file" href="/kayrx/advance/static-file.html">静态文件</a>
</details>
<details open>
<summary id="title"><strong>协议</strong></summary>
<a class="doc-section-link-protocol-websocket" href="/kayrx/protocol/websocket.html">Websocket</a>
<a class="doc-section-link-protocol-http2" href="/kayrx/protocol/http2.html">HTTP2</a>
</details>
<details open>
<summary id="title"><strong>模式</strong></summary>
<a class="doc-section-link-pattern-autoreload" href="/kayrx/pattern/autoreload.html">自动重载</a>
<a class="doc-section-link-pattern-database" href="/kayrx/pattern/database.html">数据库</a>
</details>
</details>
</div>
<div class="doc-content"> <h1 id="request-handlers">Request Handlers</h1>
<p>A request handler is an async function that accepts zero or more parameters that can be extracted
from a request (ie, <a href="https://docs.rs/actix-web/1.0.2/actix_web/trait.FromRequest.html"><em>impl FromRequest</em></a>) and returns a type that can
be converted into an HttpResponse (ie, <a href="https://docs.rs/actix-web/1.0.2/actix_web/trait.Responder.html"><em>impl Responder</em></a>).</p>
<p>Request handling happens in two stages. First the handler object is called, returning any
object that implements the <a href="https://docs.rs/actix-web/1.0.2/actix_web/trait.Responder.html"><em>Responder</em></a> trait. Then, <code>respond_to()</code> is
called on the returned object, converting itself to a <code>HttpResponse</code> or <code>Error</code>.</p>
<p>By default actix-web provides <code>Responder</code> implementations for some standard types,
such as <code>&'static str</code>, <code>String</code>, etc.</p>
<blockquote>
<p>For a complete list of implementations, check <a href="https://docs.rs/actix-web/1.0.2/actix_web/trait.Responder.html#foreign-impls"><em>Responder documentation</em></a>.</p>
</blockquote>
<p>Examples of valid handlers:</p>
<pre><code class="language-rust"><div>async <span class="hljs-function"><span class="hljs-keyword">fn</span> <span class="hljs-title">index</span></span>(_req: HttpRequest) -> &<span class="hljs-symbol">'static</span> <span class="hljs-built_in">str</span> {
<span class="hljs-string">"Hello world!"</span>
}
</div></code></pre>
<pre><code class="language-rust"><div>async <span class="hljs-function"><span class="hljs-keyword">fn</span> <span class="hljs-title">index</span></span>(_req: HttpRequest) -> <span class="hljs-built_in">String</span> {
<span class="hljs-string">"Hello world!"</span>.to_owned()
}
</div></code></pre>
<p>You can also change the signature to return <code>impl Responder</code> which works well if more
complex types are involved.</p>
<pre><code class="language-rust"><div>async <span class="hljs-function"><span class="hljs-keyword">fn</span> <span class="hljs-title">index</span></span>(_req: HttpRequest) -> <span class="hljs-keyword">impl</span> Responder {
Bytes::from_static(<span class="hljs-string">b"Hello world!"</span>)
}
</div></code></pre>
<pre><code class="language-rust"><div>async <span class="hljs-function"><span class="hljs-keyword">fn</span> <span class="hljs-title">index</span></span>(req: HttpRequest) -> <span class="hljs-built_in">Box</span><Future<Item=HttpResponse, Error=Error>> {
...
}
</div></code></pre>
<h2 id="response-with-custom-type">Response with custom type</h2>
<p>To return a custom type directly from a handler function, the type needs to implement the <code>Responder</code> trait.</p>
<p>Let's create a response for a custom type that serializes to an <code>application/json</code> response:</p>
<p>{{< include-example example="responder-trait" file="<a href="http://main.rs">main.rs</a>" section="responder-trait" >}}</p>
<h2 id="streaming-response-body">Streaming response body</h2>
<p>Response body can be generated asynchronously. In this case, body must implement
the stream trait <code>Stream<Item=Bytes, Error=Error></code>, i.e:</p>
<p>{{< include-example example="async-handlers" file="<a href="http://stream.rs">stream.rs</a>" section="stream" >}}</p>
<h2 id="different-return-types-either">Different return types (Either)</h2>
<p>Sometimes, you need to return different types of responses. For example, you can error
check and return errors, return async responses, or any result that requires two different types.</p>
<p>For this case, the <a href="https://docs.rs/actix-web/1.0.2/actix_web/enum.Either.html"><em>Either</em></a> type can be used. <code>Either</code> allows combining two
different responder types into a single type.</p>
<p>{{< include-example example="either" file="<a href="http://main.rs">main.rs</a>" section="either" >}}</p>
</div>
</div>
</div>
</div>
<div id="footer">
© 2019 <a href="https://github.com/kayrx">Kayrx</a>
</div>
</div>
<script async>
window.onload = function() {
let menu = document.getElementById('menu');
menu.addEventListener('click', function() {
let nav = document.getElementById('rnav');
if (nav.style.height == 'auto') {
nav.style.height = '0';
}else{
nav.style.height = 'auto';
}
}, false)
}
</script>
</body>
</html>