2018-10-05 17:24:17 +02:00

366 lines
114 KiB
HTML
Raw 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-US">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width,initial-scale=1">
<title>Responses | Strapi Docs</title>
<meta name="description" content="API creation made simple, secure and fast.">
<link rel="preload" href="/documentation/assets/css/styles.a8210063.css" as="style"><link rel="preload" href="/documentation/assets/js/app.a8210063.js" as="script"><link rel="preload" href="/documentation/assets/js/51.b67aee1b.js" as="script"><link rel="prefetch" href="/documentation/assets/js/28.9b077c15.js"><link rel="prefetch" href="/documentation/assets/css/1.styles.77d89b12.css"><link rel="prefetch" href="/documentation/assets/js/1.77d89b12.js"><link rel="prefetch" href="/documentation/assets/css/2.styles.08038ddb.css"><link rel="prefetch" href="/documentation/assets/js/2.08038ddb.js"><link rel="prefetch" href="/documentation/assets/js/3.4d92d5e3.js"><link rel="prefetch" href="/documentation/assets/js/4.385ae6a0.js"><link rel="prefetch" href="/documentation/assets/js/5.edd21cb3.js"><link rel="prefetch" href="/documentation/assets/js/6.fd6e4b24.js"><link rel="prefetch" href="/documentation/assets/js/7.e1f45fa9.js"><link rel="prefetch" href="/documentation/assets/js/8.dc78e1c2.js"><link rel="prefetch" href="/documentation/assets/js/9.fd6a21e6.js"><link rel="prefetch" href="/documentation/assets/js/10.f9e7d997.js"><link rel="prefetch" href="/documentation/assets/js/11.bfb9de0f.js"><link rel="prefetch" href="/documentation/assets/js/12.124227d1.js"><link rel="prefetch" href="/documentation/assets/js/13.d8092700.js"><link rel="prefetch" href="/documentation/assets/js/14.7cfd4cb8.js"><link rel="prefetch" href="/documentation/assets/js/15.55a20f7c.js"><link rel="prefetch" href="/documentation/assets/js/16.5c84c402.js"><link rel="prefetch" href="/documentation/assets/js/17.1c93d494.js"><link rel="prefetch" href="/documentation/assets/js/18.e1f1758f.js"><link rel="prefetch" href="/documentation/assets/js/19.8d1b00cd.js"><link rel="prefetch" href="/documentation/assets/js/20.856d7bdd.js"><link rel="prefetch" href="/documentation/assets/js/21.6f851286.js"><link rel="prefetch" href="/documentation/assets/js/22.7ddb4e1d.js"><link rel="prefetch" href="/documentation/assets/js/23.15e5a0c3.js"><link rel="prefetch" href="/documentation/assets/js/24.6fdf34d0.js"><link rel="prefetch" href="/documentation/assets/js/25.21c9a549.js"><link rel="prefetch" href="/documentation/assets/js/26.6608295c.js"><link rel="prefetch" href="/documentation/assets/js/27.2c9596ea.js"><link rel="prefetch" href="/documentation/assets/js/29.8ea8ecc1.js"><link rel="prefetch" href="/documentation/assets/js/30.5d2829b8.js"><link rel="prefetch" href="/documentation/assets/js/31.fad00a3a.js"><link rel="prefetch" href="/documentation/assets/js/32.a6900221.js"><link rel="prefetch" href="/documentation/assets/js/33.bbfb3084.js"><link rel="prefetch" href="/documentation/assets/js/34.0eb2f8aa.js"><link rel="prefetch" href="/documentation/assets/js/35.76c29241.js"><link rel="prefetch" href="/documentation/assets/js/36.dfcc07a1.js"><link rel="prefetch" href="/documentation/assets/js/37.173a8112.js"><link rel="prefetch" href="/documentation/assets/js/38.b86fac79.js"><link rel="prefetch" href="/documentation/assets/js/39.a7d50afe.js"><link rel="prefetch" href="/documentation/assets/js/40.0415492d.js"><link rel="prefetch" href="/documentation/assets/js/41.5ef681df.js"><link rel="prefetch" href="/documentation/assets/js/42.fdd80522.js"><link rel="prefetch" href="/documentation/assets/js/43.03b496f2.js"><link rel="prefetch" href="/documentation/assets/js/44.3f55a367.js"><link rel="prefetch" href="/documentation/assets/js/45.6746c3dc.js"><link rel="prefetch" href="/documentation/assets/js/46.ff6bc353.js"><link rel="prefetch" href="/documentation/assets/js/47.53b7147a.js"><link rel="prefetch" href="/documentation/assets/js/48.31a883aa.js"><link rel="prefetch" href="/documentation/assets/js/49.18bd1a60.js"><link rel="prefetch" href="/documentation/assets/js/50.9b6079cd.js"><link rel="prefetch" href="/documentation/assets/js/52.62bc63b6.js"><link rel="prefetch" href="/documentation/assets/js/53.2c567c55.js"><link rel="prefetch" href="/documentation/assets/js/54.6cc10d25.js"><link rel="prefetch" href="/documentation/assets/js/55.7faca13f.js"><link rel="prefetch" href="/documentation/assets/js/56.0b06ad54.js"><link rel="prefetch" href="/documentation/assets/js/57.a87c48df.js"><link rel="prefetch" href="/documentation/assets/js/58.1c6547d9.js"><link rel="prefetch" href="/documentation/assets/js/59.06f908d7.js"><link rel="prefetch" href="/documentation/assets/js/60.b5156b8d.js"><link rel="prefetch" href="/documentation/assets/js/61.1d5cafa9.js"><link rel="prefetch" href="/documentation/assets/js/62.ccacebe2.js"><link rel="prefetch" href="/documentation/assets/js/63.66946dde.js"><link rel="prefetch" href="/documentation/assets/js/64.d53bdeb7.js"><link rel="prefetch" href="/documentation/assets/js/65.0cd2da1f.js"><link rel="prefetch" href="/documentation/assets/js/66.03059dce.js"><link rel="prefetch" href="/documentation/assets/js/67.6f988923.js"><link rel="prefetch" href="/documentation/assets/js/68.303c1a05.js"><link rel="prefetch" href="/documentation/assets/js/69.3f4ed952.js"><link rel="prefetch" href="/documentation/assets/js/70.531d76be.js"><link rel="prefetch" href="/documentation/assets/js/71.ab3e74fc.js">
<link rel="stylesheet" href="/documentation/assets/css/styles.a8210063.css"><link rel="stylesheet" href="/documentation/assets/css/1.styles.77d89b12.css"><link rel="stylesheet" href="/documentation/assets/css/2.styles.08038ddb.css">
</head>
<body>
<div id="app" data-server-rendered="true"><div class="theme-container"><header class="navbar"><div class="sidebar-button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" role="img" viewBox="0 0 448 512" class="icon"><path fill="currentColor" d="M436 124H12c-6.627 0-12-5.373-12-12V80c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12z"></path></svg></div> <a href="/documentation/" class="home-link router-link-active"><!----> <span class="site-name">
Strapi Docs
</span></a> <div class="links"><div class="search-box"><input aria-label="Search" autocomplete="off" spellcheck="false" value=""> <!----></div> <nav class="nav-links can-hide"> <a href="https://github.com/strapi/strapi" target="_blank" rel="noopener noreferrer" class="repo-link">
GitHub
<svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></nav></div></header> <div class="sidebar-mask"></div> <div class="sidebar"><nav class="nav-links"> <a href="https://github.com/strapi/strapi" target="_blank" rel="noopener noreferrer" class="repo-link">
GitHub
<svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></nav> <div><select class="version-selector"><option value="/3.x.x/" selected="selected">Version 3.x.x</option><option value="/1.x.x/">Version 1.x.x</option></select></div> <ul class="sidebar-links"><li><div class="sidebar-group first"><p class="sidebar-heading open"><span>UsefulLinks</span> <!----></p> <ul class="sidebar-group-items"><li><a href="/documentation/3.x.x/" class="sidebar-link">Introduction</a></li><li><a href="https://strapi.io" class="sidebar-link">Strapi Website</a></li><li><a href="https://github.com/strapi/strapi" class="sidebar-link">GitHub Repository</a></li><li><a href="https://github.com/strapi/strapi/blob/master/CONTRIBUTING.md" class="sidebar-link">Contribution Guide</a></li></ul></div></li><li><div class="sidebar-group"><p class="sidebar-heading"><span>Getting started</span> <!----></p> <ul class="sidebar-group-items"><li><a href="/documentation/3.x.x/getting-started/installation.html" class="sidebar-link">Installation</a></li><li><a href="/documentation/3.x.x/getting-started/quick-start.html" class="sidebar-link">Quick start</a></li></ul></div></li><li><div class="sidebar-group"><p class="sidebar-heading"><span>Globals</span> <!----></p> <ul class="sidebar-group-items"><li><a href="/documentation/3.x.x/api-reference/reference.html" class="sidebar-link">API Reference</a></li><li><a href="/documentation/3.x.x/cli/CLI.html" class="sidebar-link">Command Line Interface (CLI)</a></li><li><a href="/documentation/3.x.x/concepts/concepts.html" class="sidebar-link">Concepts</a></li><li><a href="/documentation/3.x.x/configurations/configurations.html" class="sidebar-link">Configurations</a></li></ul></div></li><li><div class="sidebar-group"><p class="sidebar-heading"><span>Guides</span> <!----></p> <ul class="sidebar-group-items"><li><a href="/documentation/3.x.x/guides/authentication.html" class="sidebar-link">Authentication</a></li><li><a href="/documentation/3.x.x/guides/controllers.html" class="sidebar-link">Controllers</a></li><li><a href="/documentation/3.x.x/guides/deployment.html" class="sidebar-link">Deployment</a></li><li><a href="/documentation/3.x.x/guides/email.html" class="sidebar-link">Email</a></li><li><a href="/documentation/3.x.x/guides/filters.html" class="sidebar-link">Filters</a></li><li><a href="/documentation/3.x.x/guides/graphql.html" class="sidebar-link">GraphQL</a></li><li><a href="/documentation/3.x.x/guides/i18n.html" class="sidebar-link">Internationalization</a></li><li><a href="/documentation/3.x.x/guides/models.html" class="sidebar-link">Models</a></li><li><a href="/documentation/3.x.x/guides/policies.html" class="sidebar-link">Policies</a></li><li><a href="/documentation/3.x.x/guides/public-assets.html" class="sidebar-link">Public Assets</a></li><li><a href="/documentation/3.x.x/guides/requests.html" class="sidebar-link">Request</a></li></ul></div></li><li><div class="sidebar-group"><p class="sidebar-heading"><span>Advanced</span> <!----></p> <ul class="sidebar-group-items"><li><a href="/documentation/3.x.x/advanced/customize-admin.html" class="sidebar-link">Admin panel</a></li><li><a href="/documentation/3.x.x/advanced/hooks.html" class="sidebar-link">Hooks</a></li><li><a href="/documentation/3.x.x/advanced/logging.html" class="sidebar-link">Logging</a></li><li><a href="/documentation/3.x.x/advanced/middlewares.html" class="sidebar-link">Middlewares</a></li><li><a href="/documentation/3.x.x/advanced/usage-tracking.html" class="sidebar-link">Usage tracking</a></li></ul></div></li><li><div class="sidebar-group"><p class="sidebar-heading"><span>Help</span> <!----></p> <ul class="sidebar-group-items"><li><a href="https://github.com/strapi/strapi/wiki" class="sidebar-link">Migration guides</a></li><li><a href="/documentation/3.x.x/tutorials/" class="sidebar-link">Tutorials</a></li></ul></div></li></ul> </div> <div class="page"><div class="content"><h1 id="responses"><a href="#responses" aria-hidden="true" class="header-anchor">#</a> Responses</h1> <p>See the <a href="/documentation/3.x.x/concepts/concepts.html#responses">responses concepts</a> for details.</p> <h3 id="api-reference"><a href="#api-reference" aria-hidden="true" class="header-anchor">#</a> API Reference</h3> <p>For more information, please refer to the <a href="http://koajs.com/#response" target="_blank" rel="noopener noreferrer">Koa response documentation<svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a>.</p> <h2 id="context-response"><a href="#context-response" aria-hidden="true" class="header-anchor">#</a> Context Response</h2> <p>The context object (<code>ctx</code>) contains a list of values and functions useful to manage server responses. They are accessible through <code>ctx.response</code>, from <a href="/documentation/3.x.x/guides/controllers.html">controllers</a> and <a href="/documentation/3.x.x/guides/policies.html">policies</a>.</p> <h4 id="response-header"><a href="#response-header" aria-hidden="true" class="header-anchor">#</a> response.header</h4> <p>Response header object.</p> <h4 id="response-headers"><a href="#response-headers" aria-hidden="true" class="header-anchor">#</a> response.headers</h4> <p>Response header object. Alias as <code>response.header</code>.</p> <h4 id="response-socket"><a href="#response-socket" aria-hidden="true" class="header-anchor">#</a> response.socket</h4> <p>Request socket.</p> <h4 id="response-status"><a href="#response-status" aria-hidden="true" class="header-anchor">#</a> response.status</h4> <p>Get response status. By default, <code>response.status</code> is set to <code>404</code> unlike node's <code>res.statusCode</code> which defaults to <code>200</code>.</p> <h4 id="response-status-2"><a href="#response-status-2" aria-hidden="true" class="header-anchor">#</a> response.status=</h4> <p>Set response status via numeric code:</p> <ul><li>100 &quot;continue&quot;</li> <li>101 &quot;switching protocols&quot;</li> <li>102 &quot;processing&quot;</li> <li>200 &quot;ok&quot;</li> <li>201 &quot;created&quot;</li> <li>202 &quot;accepted&quot;</li> <li>203 &quot;non-authoritative information&quot;</li> <li>204 &quot;no content&quot;</li> <li>205 &quot;reset content&quot;</li> <li>206 &quot;partial content&quot;</li> <li>207 &quot;multi-status&quot;</li> <li>208 &quot;already reported&quot;</li> <li>226 &quot;im used&quot;</li> <li>300 &quot;multiple choices&quot;</li> <li>301 &quot;moved permanently&quot;</li> <li>302 &quot;found&quot;</li> <li>303 &quot;see other&quot;</li> <li>304 &quot;not modified&quot;</li> <li>305 &quot;use proxy&quot;</li> <li>307 &quot;temporary redirect&quot;</li> <li>308 &quot;permanent redirect&quot;</li> <li>400 &quot;bad request&quot;</li> <li>401 &quot;unauthorized&quot;</li> <li>402 &quot;payment required&quot;</li> <li>403 &quot;forbidden&quot;</li> <li>404 &quot;not found&quot;</li> <li>405 &quot;method not allowed&quot;</li> <li>406 &quot;not acceptable&quot;</li> <li>407 &quot;proxy authentication required&quot;</li> <li>408 &quot;request timeout&quot;</li> <li>409 &quot;conflict&quot;</li> <li>410 &quot;gone&quot;</li> <li>411 &quot;length required&quot;</li> <li>412 &quot;precondition failed&quot;</li> <li>413 &quot;payload too large&quot;</li> <li>414 &quot;uri too long&quot;</li> <li>415 &quot;unsupported media type&quot;</li> <li>416 &quot;range not satisfiable&quot;</li> <li>417 &quot;expectation failed&quot;</li> <li>418 &quot;I'm a teapot&quot;</li> <li>422 &quot;unprocessable entity&quot;</li> <li>423 &quot;locked&quot;</li> <li>424 &quot;failed dependency&quot;</li> <li>426 &quot;upgrade required&quot;</li> <li>428 &quot;precondition required&quot;</li> <li>429 &quot;too many requests&quot;</li> <li>431 &quot;request header fields too large&quot;</li> <li>500 &quot;internal server error&quot;</li> <li>501 &quot;not implemented&quot;</li> <li>502 &quot;bad gateway&quot;</li> <li>503 &quot;service unavailable&quot;</li> <li>504 &quot;gateway timeout&quot;</li> <li>505 &quot;http version not supported&quot;</li> <li>506 &quot;variant also negotiates&quot;</li> <li>507 &quot;insufficient storage&quot;</li> <li>508 &quot;loop detected&quot;</li> <li>510 &quot;not extended&quot;</li> <li>511 &quot;network authentication required&quot;</li></ul> <p><strong>NOTE</strong>: don't worry too much about memorizing these strings,
if you have a typo an error will be thrown, displaying this list
so you can make a correction.</p> <h4 id="response-message"><a href="#response-message" aria-hidden="true" class="header-anchor">#</a> response.message</h4> <p>Get response status message. By default, <code>response.message</code> is
associated with <code>response.status</code>.</p> <h4 id="response-message-2"><a href="#response-message-2" aria-hidden="true" class="header-anchor">#</a> response.message=</h4> <p>Set response status message to the given value.</p> <h4 id="response-length"><a href="#response-length" aria-hidden="true" class="header-anchor">#</a> response.length=</h4> <p>Set response Content-Length to the given value.</p> <h4 id="response-length-2"><a href="#response-length-2" aria-hidden="true" class="header-anchor">#</a> response.length</h4> <p>Return response Content-Length as a number when present, or deduce
from <code>ctx.body</code> when possible, or <code>undefined</code>.</p> <h4 id="response-body"><a href="#response-body" aria-hidden="true" class="header-anchor">#</a> response.body</h4> <p>Get response body.</p> <h4 id="response-body-2"><a href="#response-body-2" aria-hidden="true" class="header-anchor">#</a> response.body=</h4> <p>Set response body to one of the following:</p> <ul><li><code>string</code> written</li> <li><code>Buffer</code> written</li> <li><code>Stream</code> piped</li> <li><code>Object</code> || <code>Array</code> json-stringified</li> <li><code>null</code> no content response</li></ul> <p>If <code>response.status</code> has not been set, Koa will automatically set the status to <code>200</code> or <code>204</code>.</p> <h5 id="string"><a href="#string" aria-hidden="true" class="header-anchor">#</a> String</h5> <p>The Content-Type is defaulted to text/html or text/plain, both with
a default charset of utf-8. The Content-Length field is also set.</p> <h5 id="buffer"><a href="#buffer" aria-hidden="true" class="header-anchor">#</a> Buffer</h5> <p>The Content-Type is defaulted to application/octet-stream, and Content-Length
is also set.</p> <h5 id="stream"><a href="#stream" aria-hidden="true" class="header-anchor">#</a> Stream</h5> <p>The Content-Type is defaulted to application/octet-stream.</p> <p>Whenever a stream is set as the response body, <code>.onerror</code> is automatically added as a listener to the <code>error</code> event to catch any errors.
In addition, whenever the request is closed (even prematurely), the stream is destroyed.
If you do not want these two features, do not set the stream as the body directly.
For example, you may not want this when setting the body as an HTTP stream in a proxy as it would destroy the underlying connection.</p> <p>See: https://github.com/koajs/koa/pull/612 for more information.</p> <p>Here's an example of stream error handling without automatically destroying the stream:</p> <div class="language-js extra-class"><pre class="language-js"><code><span class="token keyword">const</span> PassThrough <span class="token operator">=</span> <span class="token function">require</span><span class="token punctuation">(</span><span class="token string">'stream'</span><span class="token punctuation">)</span><span class="token punctuation">.</span>PassThrough<span class="token punctuation">;</span>
app<span class="token punctuation">.</span><span class="token function">use</span><span class="token punctuation">(</span><span class="token keyword">async</span> ctx <span class="token operator">=&gt;</span> <span class="token punctuation">{</span>
ctx<span class="token punctuation">.</span>body <span class="token operator">=</span> someHTTPStream<span class="token punctuation">.</span><span class="token function">on</span><span class="token punctuation">(</span><span class="token string">'error'</span><span class="token punctuation">,</span> ctx<span class="token punctuation">.</span>onerror<span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">pipe</span><span class="token punctuation">(</span><span class="token function">PassThrough</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre></div><h5 id="object"><a href="#object" aria-hidden="true" class="header-anchor">#</a> Object</h5> <p>The Content-Type is defaulted to application/json. This includes plain objects <code>{ foo: 'bar' }</code> and arrays <code>['foo', 'bar']</code>.</p> <h4 id="response-get-field"><a href="#response-get-field" aria-hidden="true" class="header-anchor">#</a> response.get(field)</h4> <p>Get a response header field value with case-insensitive <code>field</code>.</p> <div class="language-js extra-class"><pre class="language-js"><code><span class="token keyword">const</span> etag <span class="token operator">=</span> ctx<span class="token punctuation">.</span>response<span class="token punctuation">.</span><span class="token keyword">get</span><span class="token punctuation">(</span><span class="token string">'ETag'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre></div><h4 id="response-set-field-value"><a href="#response-set-field-value" aria-hidden="true" class="header-anchor">#</a> response.set(field, value)</h4> <p>Set response header <code>field</code> to <code>value</code>:</p> <div class="language-js extra-class"><pre class="language-js"><code>ctx<span class="token punctuation">.</span><span class="token keyword">set</span><span class="token punctuation">(</span><span class="token string">'Cache-Control'</span><span class="token punctuation">,</span> <span class="token string">'no-cache'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre></div><h4 id="response-append-field-value"><a href="#response-append-field-value" aria-hidden="true" class="header-anchor">#</a> response.append(field, value)</h4> <p>Append additional header <code>field</code> with value <code>val</code>.</p> <div class="language-js extra-class"><pre class="language-js"><code>ctx<span class="token punctuation">.</span><span class="token function">append</span><span class="token punctuation">(</span><span class="token string">'Link'</span><span class="token punctuation">,</span> <span class="token string">'&lt;http://127.0.0.1/&gt;'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre></div><h4 id="response-set-fields"><a href="#response-set-fields" aria-hidden="true" class="header-anchor">#</a> response.set(fields)</h4> <p>Set several response header <code>fields</code> with an object:</p> <div class="language-js extra-class"><pre class="language-js"><code>ctx<span class="token punctuation">.</span><span class="token keyword">set</span><span class="token punctuation">(</span><span class="token punctuation">{</span>
<span class="token string">'Etag'</span><span class="token punctuation">:</span> <span class="token string">'1234'</span><span class="token punctuation">,</span>
<span class="token string">'Last-Modified'</span><span class="token punctuation">:</span> date
<span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre></div><h4 id="response-remove-field"><a href="#response-remove-field" aria-hidden="true" class="header-anchor">#</a> response.remove(field)</h4> <p>Remove header <code>field</code>.</p> <h4 id="response-type"><a href="#response-type" aria-hidden="true" class="header-anchor">#</a> response.type</h4> <p>Get response <code>Content-Type</code> void of parameters such as &quot;charset&quot;.</p> <div class="language-js extra-class"><pre class="language-js"><code><span class="token keyword">const</span> ct <span class="token operator">=</span> ctx<span class="token punctuation">.</span>type<span class="token punctuation">;</span>
<span class="token comment">// =&gt; &quot;image/png&quot;</span>
</code></pre></div><h4 id="response-type-2"><a href="#response-type-2" aria-hidden="true" class="header-anchor">#</a> response.type=</h4> <p>Set response <code>Content-Type</code> via mime string or file extension.</p> <div class="language-js extra-class"><pre class="language-js"><code>ctx<span class="token punctuation">.</span>type <span class="token operator">=</span> <span class="token string">'text/plain; charset=utf-8'</span><span class="token punctuation">;</span>
ctx<span class="token punctuation">.</span>type <span class="token operator">=</span> <span class="token string">'image/png'</span><span class="token punctuation">;</span>
ctx<span class="token punctuation">.</span>type <span class="token operator">=</span> <span class="token string">'.png'</span><span class="token punctuation">;</span>
ctx<span class="token punctuation">.</span>type <span class="token operator">=</span> <span class="token string">'png'</span><span class="token punctuation">;</span>
</code></pre></div><div class="note custom-block"><p>when appropriate a <code>charset</code> is selected for you, for
example <code>response.type = 'html'</code> will default to &quot;utf-8&quot;. If you need to overwrite <code>charset</code>,
use <code>ctx.set('Content-Type', 'text/html')</code> to set response header field to value directly.</p></div> <h4 id="response-is-types"><a href="#response-is-types" aria-hidden="true" class="header-anchor">#</a> response.is(types...)</h4> <p>Very similar to <code>ctx.request.is()</code>.
Check whether the response type is one of the supplied types.
This is particularly useful for creating middleware that
manipulate responses.</p> <p>For example, this is a middleware that minifies
all HTML responses except for streams.</p> <div class="language-js extra-class"><pre class="language-js"><code><span class="token keyword">const</span> minify <span class="token operator">=</span> <span class="token function">require</span><span class="token punctuation">(</span><span class="token string">'html-minifier'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
app<span class="token punctuation">.</span><span class="token function">use</span><span class="token punctuation">(</span><span class="token keyword">async</span> <span class="token punctuation">(</span>ctx<span class="token punctuation">,</span> next<span class="token punctuation">)</span> <span class="token operator">=&gt;</span> <span class="token punctuation">{</span>
<span class="token keyword">await</span> <span class="token function">next</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token operator">!</span>ctx<span class="token punctuation">.</span>response<span class="token punctuation">.</span><span class="token function">is</span><span class="token punctuation">(</span><span class="token string">'html'</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token keyword">return</span><span class="token punctuation">;</span>
<span class="token keyword">let</span> body <span class="token operator">=</span> ctx<span class="token punctuation">.</span>body<span class="token punctuation">;</span>
<span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token operator">!</span>body <span class="token operator">||</span> body<span class="token punctuation">.</span>pipe<span class="token punctuation">)</span> <span class="token keyword">return</span><span class="token punctuation">;</span>
<span class="token keyword">if</span> <span class="token punctuation">(</span>Buffer<span class="token punctuation">.</span><span class="token function">isBuffer</span><span class="token punctuation">(</span>body<span class="token punctuation">)</span><span class="token punctuation">)</span> body <span class="token operator">=</span> body<span class="token punctuation">.</span><span class="token function">toString</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
ctx<span class="token punctuation">.</span>body <span class="token operator">=</span> <span class="token function">minify</span><span class="token punctuation">(</span>body<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre></div><h4 id="response-redirect-url-alt"><a href="#response-redirect-url-alt" aria-hidden="true" class="header-anchor">#</a> response.redirect(url, [alt])</h4> <p>Perform a [302] redirect to <code>url</code>.</p> <p>The string &quot;back&quot; is special-cased
to provide Referrer support, when Referrer
is not present <code>alt</code> or &quot;/&quot; is used.</p> <div class="language-js extra-class"><pre class="language-js"><code>ctx<span class="token punctuation">.</span><span class="token function">redirect</span><span class="token punctuation">(</span><span class="token string">'back'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
ctx<span class="token punctuation">.</span><span class="token function">redirect</span><span class="token punctuation">(</span><span class="token string">'back'</span><span class="token punctuation">,</span> <span class="token string">'/index.html'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
ctx<span class="token punctuation">.</span><span class="token function">redirect</span><span class="token punctuation">(</span><span class="token string">'/login'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
ctx<span class="token punctuation">.</span><span class="token function">redirect</span><span class="token punctuation">(</span><span class="token string">'http://google.com'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre></div><p>To alter the default status of <code>302</code>, simply assign the status
before or after this call. To alter the body, assign it after this call:</p> <div class="language-js extra-class"><pre class="language-js"><code>ctx<span class="token punctuation">.</span>status <span class="token operator">=</span> <span class="token number">301</span><span class="token punctuation">;</span>
ctx<span class="token punctuation">.</span><span class="token function">redirect</span><span class="token punctuation">(</span><span class="token string">'/cart'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
ctx<span class="token punctuation">.</span>body <span class="token operator">=</span> <span class="token string">'Redirecting to shopping cart'</span><span class="token punctuation">;</span>
</code></pre></div><h4 id="response-attachment-filename"><a href="#response-attachment-filename" aria-hidden="true" class="header-anchor">#</a> response.attachment([filename])</h4> <p>Set <code>Content-Disposition</code> to &quot;attachment&quot; to signal the client
to prompt for download. Optionally specify the <code>filename</code> of the
download.</p> <h4 id="response-headersent"><a href="#response-headersent" aria-hidden="true" class="header-anchor">#</a> response.headerSent</h4> <p>Check if a response header has already been sent. Useful for seeing
if the client may be notified on error.</p> <h4 id="response-lastmodified"><a href="#response-lastmodified" aria-hidden="true" class="header-anchor">#</a> response.lastModified</h4> <p>Return the <code>Last-Modified</code> header as a <code>Date</code>, if it exists.</p> <h4 id="response-lastmodified-2"><a href="#response-lastmodified-2" aria-hidden="true" class="header-anchor">#</a> response.lastModified=</h4> <p>Set the <code>Last-Modified</code> header as an appropriate UTC string.
You can either set it as a <code>Date</code> or date string.</p> <div class="language-js extra-class"><pre class="language-js"><code>ctx<span class="token punctuation">.</span>response<span class="token punctuation">.</span>lastModified <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Date</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre></div><h4 id="response-etag"><a href="#response-etag" aria-hidden="true" class="header-anchor">#</a> response.etag=</h4> <p>Set the ETag of a response including the wrapped <code>&quot;</code>s.
Note that there is no corresponding <code>response.etag</code> getter.</p> <div class="language-js extra-class"><pre class="language-js"><code>ctx<span class="token punctuation">.</span>response<span class="token punctuation">.</span>etag <span class="token operator">=</span> crypto<span class="token punctuation">.</span><span class="token function">createHash</span><span class="token punctuation">(</span><span class="token string">'md5'</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">update</span><span class="token punctuation">(</span>ctx<span class="token punctuation">.</span>body<span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">digest</span><span class="token punctuation">(</span><span class="token string">'hex'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre></div><h4 id="response-vary-field"><a href="#response-vary-field" aria-hidden="true" class="header-anchor">#</a> response.vary(field)</h4> <p>Vary on <code>field</code>.</p> <h4 id="response-flushheaders"><a href="#response-flushheaders" aria-hidden="true" class="header-anchor">#</a> response.flushHeaders()</h4> <p>Flush any set headers, and begin the body.</p> <h2 id="response"><a href="#response" aria-hidden="true" class="header-anchor">#</a> Response</h2> <p>A Koa <code>Response</code> object is an abstraction on top of node's vanilla response object,
providing additional functionality that is useful for every day HTTP server
development.</p> <h3 id="api"><a href="#api" aria-hidden="true" class="header-anchor">#</a> API</h3> <h4 id="response-header-2"><a href="#response-header-2" aria-hidden="true" class="header-anchor">#</a> response.header</h4> <p>Response header object.</p> <h4 id="response-headers-2"><a href="#response-headers-2" aria-hidden="true" class="header-anchor">#</a> response.headers</h4> <p>Response header object. Alias as <code>response.header</code>.</p> <h4 id="response-socket-2"><a href="#response-socket-2" aria-hidden="true" class="header-anchor">#</a> response.socket</h4> <p>Request socket.</p> <h4 id="response-status-3"><a href="#response-status-3" aria-hidden="true" class="header-anchor">#</a> response.status</h4> <p>Get response status. By default, <code>response.status</code> is set to <code>404</code> unlike node's <code>res.statusCode</code> which defaults to <code>200</code>.</p> <h4 id="response-status-4"><a href="#response-status-4" aria-hidden="true" class="header-anchor">#</a> response.status=</h4> <p>Set response status via numeric code:</p> <ul><li>100 &quot;continue&quot;</li> <li>101 &quot;switching protocols&quot;</li> <li>102 &quot;processing&quot;</li> <li>200 &quot;ok&quot;</li> <li>201 &quot;created&quot;</li> <li>202 &quot;accepted&quot;</li> <li>203 &quot;non-authoritative information&quot;</li> <li>204 &quot;no content&quot;</li> <li>205 &quot;reset content&quot;</li> <li>206 &quot;partial content&quot;</li> <li>207 &quot;multi-status&quot;</li> <li>208 &quot;already reported&quot;</li> <li>226 &quot;im used&quot;</li> <li>300 &quot;multiple choices&quot;</li> <li>301 &quot;moved permanently&quot;</li> <li>302 &quot;found&quot;</li> <li>303 &quot;see other&quot;</li> <li>304 &quot;not modified&quot;</li> <li>305 &quot;use proxy&quot;</li> <li>307 &quot;temporary redirect&quot;</li> <li>308 &quot;permanent redirect&quot;</li> <li>400 &quot;bad request&quot;</li> <li>401 &quot;unauthorized&quot;</li> <li>402 &quot;payment required&quot;</li> <li>403 &quot;forbidden&quot;</li> <li>404 &quot;not found&quot;</li> <li>405 &quot;method not allowed&quot;</li> <li>406 &quot;not acceptable&quot;</li> <li>407 &quot;proxy authentication required&quot;</li> <li>408 &quot;request timeout&quot;</li> <li>409 &quot;conflict&quot;</li> <li>410 &quot;gone&quot;</li> <li>411 &quot;length required&quot;</li> <li>412 &quot;precondition failed&quot;</li> <li>413 &quot;payload too large&quot;</li> <li>414 &quot;uri too long&quot;</li> <li>415 &quot;unsupported media type&quot;</li> <li>416 &quot;range not satisfiable&quot;</li> <li>417 &quot;expectation failed&quot;</li> <li>418 &quot;I'm a teapot&quot;</li> <li>422 &quot;unprocessable entity&quot;</li> <li>423 &quot;locked&quot;</li> <li>424 &quot;failed dependency&quot;</li> <li>426 &quot;upgrade required&quot;</li> <li>428 &quot;precondition required&quot;</li> <li>429 &quot;too many requests&quot;</li> <li>431 &quot;request header fields too large&quot;</li> <li>500 &quot;internal server error&quot;</li> <li>501 &quot;not implemented&quot;</li> <li>502 &quot;bad gateway&quot;</li> <li>503 &quot;service unavailable&quot;</li> <li>504 &quot;gateway timeout&quot;</li> <li>505 &quot;http version not supported&quot;</li> <li>506 &quot;variant also negotiates&quot;</li> <li>507 &quot;insufficient storage&quot;</li> <li>508 &quot;loop detected&quot;</li> <li>510 &quot;not extended&quot;</li> <li>511 &quot;network authentication required&quot;</li></ul> <p><strong>NOTE</strong>: don't worry too much about memorizing these strings,
if you have a typo an error will be thrown, displaying this list
so you can make a correction.</p> <h4 id="response-message-3"><a href="#response-message-3" aria-hidden="true" class="header-anchor">#</a> response.message</h4> <p>Get response status message. By default, <code>response.message</code> is
associated with <code>response.status</code>.</p> <h4 id="response-message-4"><a href="#response-message-4" aria-hidden="true" class="header-anchor">#</a> response.message=</h4> <p>Set response status message to the given value.</p> <h4 id="response-length-3"><a href="#response-length-3" aria-hidden="true" class="header-anchor">#</a> response.length=</h4> <p>Set response Content-Length to the given value.</p> <h4 id="response-length-4"><a href="#response-length-4" aria-hidden="true" class="header-anchor">#</a> response.length</h4> <p>Return response Content-Length as a number when present, or deduce
from <code>ctx.body</code> when possible, or <code>undefined</code>.</p> <h4 id="response-body-3"><a href="#response-body-3" aria-hidden="true" class="header-anchor">#</a> response.body</h4> <p>Get response body.</p> <h4 id="response-body-4"><a href="#response-body-4" aria-hidden="true" class="header-anchor">#</a> response.body=</h4> <p>Set response body to one of the following:</p> <ul><li><code>string</code> written</li> <li><code>Buffer</code> written</li> <li><code>Stream</code> piped</li> <li><code>Object</code> || <code>Array</code> json-stringified</li> <li><code>null</code> no content response</li></ul> <p>If <code>response.status</code> has not been set, Koa will automatically set the status to <code>200</code> or <code>204</code>.</p> <h5 id="string-2"><a href="#string-2" aria-hidden="true" class="header-anchor">#</a> String</h5> <p>The Content-Type is defaulted to text/html or text/plain, both with
a default charset of utf-8. The Content-Length field is also set.</p> <h5 id="buffer-2"><a href="#buffer-2" aria-hidden="true" class="header-anchor">#</a> Buffer</h5> <p>The Content-Type is defaulted to application/octet-stream, and Content-Length
is also set.</p> <h5 id="stream-2"><a href="#stream-2" aria-hidden="true" class="header-anchor">#</a> Stream</h5> <p>The Content-Type is defaulted to application/octet-stream.</p> <p>Whenever a stream is set as the response body, <code>.onerror</code> is automatically added as a listener to the <code>error</code> event to catch any errors.
In addition, whenever the request is closed (even prematurely), the stream is destroyed.
If you do not want these two features, do not set the stream as the body directly.
For example, you may not want this when setting the body as an HTTP stream in a proxy as it would destroy the underlying connection.</p> <p>See: https://github.com/koajs/koa/pull/612 for more information.</p> <p>Here's an example of stream error handling without automatically destroying the stream:</p> <div class="language-js extra-class"><pre class="language-js"><code><span class="token keyword">const</span> PassThrough <span class="token operator">=</span> <span class="token function">require</span><span class="token punctuation">(</span><span class="token string">'stream'</span><span class="token punctuation">)</span><span class="token punctuation">.</span>PassThrough<span class="token punctuation">;</span>
app<span class="token punctuation">.</span><span class="token function">use</span><span class="token punctuation">(</span><span class="token keyword">async</span> ctx <span class="token operator">=&gt;</span> <span class="token punctuation">{</span>
ctx<span class="token punctuation">.</span>body <span class="token operator">=</span> someHTTPStream<span class="token punctuation">.</span><span class="token function">on</span><span class="token punctuation">(</span><span class="token string">'error'</span><span class="token punctuation">,</span> ctx<span class="token punctuation">.</span>onerror<span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">pipe</span><span class="token punctuation">(</span><span class="token function">PassThrough</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre></div><h5 id="object-2"><a href="#object-2" aria-hidden="true" class="header-anchor">#</a> Object</h5> <p>The Content-Type is defaulted to application/json. This includes plain objects <code>{ foo: 'bar' }</code> and arrays <code>['foo', 'bar']</code>.</p> <h4 id="response-get-field-2"><a href="#response-get-field-2" aria-hidden="true" class="header-anchor">#</a> response.get(field)</h4> <p>Get a response header field value with case-insensitive <code>field</code>.</p> <div class="language-js extra-class"><pre class="language-js"><code><span class="token keyword">const</span> etag <span class="token operator">=</span> ctx<span class="token punctuation">.</span>response<span class="token punctuation">.</span><span class="token keyword">get</span><span class="token punctuation">(</span><span class="token string">'ETag'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre></div><h4 id="response-set-field-value-2"><a href="#response-set-field-value-2" aria-hidden="true" class="header-anchor">#</a> response.set(field, value)</h4> <p>Set response header <code>field</code> to <code>value</code>:</p> <div class="language-js extra-class"><pre class="language-js"><code>ctx<span class="token punctuation">.</span><span class="token keyword">set</span><span class="token punctuation">(</span><span class="token string">'Cache-Control'</span><span class="token punctuation">,</span> <span class="token string">'no-cache'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre></div><h4 id="response-append-field-value-2"><a href="#response-append-field-value-2" aria-hidden="true" class="header-anchor">#</a> response.append(field, value)</h4> <p>Append additional header <code>field</code> with value <code>val</code>.</p> <div class="language-js extra-class"><pre class="language-js"><code>ctx<span class="token punctuation">.</span><span class="token function">append</span><span class="token punctuation">(</span><span class="token string">'Link'</span><span class="token punctuation">,</span> <span class="token string">'&lt;http://127.0.0.1/&gt;'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre></div><h4 id="response-set-fields-2"><a href="#response-set-fields-2" aria-hidden="true" class="header-anchor">#</a> response.set(fields)</h4> <p>Set several response header <code>fields</code> with an object:</p> <div class="language-js extra-class"><pre class="language-js"><code>ctx<span class="token punctuation">.</span><span class="token keyword">set</span><span class="token punctuation">(</span><span class="token punctuation">{</span>
<span class="token string">'Etag'</span><span class="token punctuation">:</span> <span class="token string">'1234'</span><span class="token punctuation">,</span>
<span class="token string">'Last-Modified'</span><span class="token punctuation">:</span> date
<span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre></div><h4 id="response-remove-field-2"><a href="#response-remove-field-2" aria-hidden="true" class="header-anchor">#</a> response.remove(field)</h4> <p>Remove header <code>field</code>.</p> <h4 id="response-type-3"><a href="#response-type-3" aria-hidden="true" class="header-anchor">#</a> response.type</h4> <p>Get response <code>Content-Type</code> void of parameters such as &quot;charset&quot;.</p> <div class="language-js extra-class"><pre class="language-js"><code><span class="token keyword">const</span> ct <span class="token operator">=</span> ctx<span class="token punctuation">.</span>type<span class="token punctuation">;</span>
<span class="token comment">// =&gt; &quot;image/png&quot;</span>
</code></pre></div><h4 id="response-type-4"><a href="#response-type-4" aria-hidden="true" class="header-anchor">#</a> response.type=</h4> <p>Set response <code>Content-Type</code> via mime string or file extension.</p> <div class="language-js extra-class"><pre class="language-js"><code>ctx<span class="token punctuation">.</span>type <span class="token operator">=</span> <span class="token string">'text/plain; charset=utf-8'</span><span class="token punctuation">;</span>
ctx<span class="token punctuation">.</span>type <span class="token operator">=</span> <span class="token string">'image/png'</span><span class="token punctuation">;</span>
ctx<span class="token punctuation">.</span>type <span class="token operator">=</span> <span class="token string">'.png'</span><span class="token punctuation">;</span>
ctx<span class="token punctuation">.</span>type <span class="token operator">=</span> <span class="token string">'png'</span><span class="token punctuation">;</span>
</code></pre></div><div class="note custom-block"><p>when appropriate a <code>charset</code> is selected for you, for
example <code>response.type = 'html'</code> will default to &quot;utf-8&quot;. If you need to overwrite <code>charset</code>,
use <code>ctx.set('Content-Type', 'text/html')</code> to set response header field to value directly.</p></div> <h4 id="response-is-types-2"><a href="#response-is-types-2" aria-hidden="true" class="header-anchor">#</a> response.is(types...)</h4> <p>Very similar to <code>ctx.request.is()</code>.
Check whether the response type is one of the supplied types.
This is particularly useful for creating middleware that
manipulate responses.</p> <p>For example, this is a middleware that minifies
all HTML responses except for streams.</p> <div class="language-js extra-class"><pre class="language-js"><code><span class="token keyword">const</span> minify <span class="token operator">=</span> <span class="token function">require</span><span class="token punctuation">(</span><span class="token string">'html-minifier'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
app<span class="token punctuation">.</span><span class="token function">use</span><span class="token punctuation">(</span><span class="token keyword">async</span> <span class="token punctuation">(</span>ctx<span class="token punctuation">,</span> next<span class="token punctuation">)</span> <span class="token operator">=&gt;</span> <span class="token punctuation">{</span>
<span class="token keyword">await</span> <span class="token function">next</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token operator">!</span>ctx<span class="token punctuation">.</span>response<span class="token punctuation">.</span><span class="token function">is</span><span class="token punctuation">(</span><span class="token string">'html'</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token keyword">return</span><span class="token punctuation">;</span>
<span class="token keyword">let</span> body <span class="token operator">=</span> ctx<span class="token punctuation">.</span>body<span class="token punctuation">;</span>
<span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token operator">!</span>body <span class="token operator">||</span> body<span class="token punctuation">.</span>pipe<span class="token punctuation">)</span> <span class="token keyword">return</span><span class="token punctuation">;</span>
<span class="token keyword">if</span> <span class="token punctuation">(</span>Buffer<span class="token punctuation">.</span><span class="token function">isBuffer</span><span class="token punctuation">(</span>body<span class="token punctuation">)</span><span class="token punctuation">)</span> body <span class="token operator">=</span> body<span class="token punctuation">.</span><span class="token function">toString</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
ctx<span class="token punctuation">.</span>body <span class="token operator">=</span> <span class="token function">minify</span><span class="token punctuation">(</span>body<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre></div><h4 id="response-redirect-url-alt-2"><a href="#response-redirect-url-alt-2" aria-hidden="true" class="header-anchor">#</a> response.redirect(url, [alt])</h4> <p>Perform a [302] redirect to <code>url</code>.</p> <p>The string &quot;back&quot; is special-cased
to provide Referrer support, when Referrer
is not present <code>alt</code> or &quot;/&quot; is used.</p> <div class="language-js extra-class"><pre class="language-js"><code>ctx<span class="token punctuation">.</span><span class="token function">redirect</span><span class="token punctuation">(</span><span class="token string">'back'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
ctx<span class="token punctuation">.</span><span class="token function">redirect</span><span class="token punctuation">(</span><span class="token string">'back'</span><span class="token punctuation">,</span> <span class="token string">'/index.html'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
ctx<span class="token punctuation">.</span><span class="token function">redirect</span><span class="token punctuation">(</span><span class="token string">'/login'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
ctx<span class="token punctuation">.</span><span class="token function">redirect</span><span class="token punctuation">(</span><span class="token string">'http://google.com'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre></div><p>To alter the default status of <code>302</code>, simply assign the status
before or after this call. To alter the body, assign it after this call:</p> <div class="language-js extra-class"><pre class="language-js"><code>ctx<span class="token punctuation">.</span>status <span class="token operator">=</span> <span class="token number">301</span><span class="token punctuation">;</span>
ctx<span class="token punctuation">.</span><span class="token function">redirect</span><span class="token punctuation">(</span><span class="token string">'/cart'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
ctx<span class="token punctuation">.</span>body <span class="token operator">=</span> <span class="token string">'Redirecting to shopping cart'</span><span class="token punctuation">;</span>
</code></pre></div><h4 id="response-attachment-filename-2"><a href="#response-attachment-filename-2" aria-hidden="true" class="header-anchor">#</a> response.attachment([filename])</h4> <p>Set <code>Content-Disposition</code> to &quot;attachment&quot; to signal the client
to prompt for download. Optionally specify the <code>filename</code> of the
download.</p> <h4 id="response-headersent-2"><a href="#response-headersent-2" aria-hidden="true" class="header-anchor">#</a> response.headerSent</h4> <p>Check if a response header has already been sent. Useful for seeing
if the client may be notified on error.</p> <h4 id="response-lastmodified-3"><a href="#response-lastmodified-3" aria-hidden="true" class="header-anchor">#</a> response.lastModified</h4> <p>Return the <code>Last-Modified</code> header as a <code>Date</code>, if it exists.</p> <h4 id="response-lastmodified-4"><a href="#response-lastmodified-4" aria-hidden="true" class="header-anchor">#</a> response.lastModified=</h4> <p>Set the <code>Last-Modified</code> header as an appropriate UTC string.
You can either set it as a <code>Date</code> or date string.</p> <div class="language-js extra-class"><pre class="language-js"><code>ctx<span class="token punctuation">.</span>response<span class="token punctuation">.</span>lastModified <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Date</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre></div><h4 id="response-etag-2"><a href="#response-etag-2" aria-hidden="true" class="header-anchor">#</a> response.etag=</h4> <p>Set the ETag of a response including the wrapped <code>&quot;</code>s.
Note that there is no corresponding <code>response.etag</code> getter.</p> <div class="language-js extra-class"><pre class="language-js"><code>ctx<span class="token punctuation">.</span>response<span class="token punctuation">.</span>etag <span class="token operator">=</span> crypto<span class="token punctuation">.</span><span class="token function">createHash</span><span class="token punctuation">(</span><span class="token string">'md5'</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">update</span><span class="token punctuation">(</span>ctx<span class="token punctuation">.</span>body<span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">digest</span><span class="token punctuation">(</span><span class="token string">'hex'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre></div><h4 id="response-vary-field-2"><a href="#response-vary-field-2" aria-hidden="true" class="header-anchor">#</a> response.vary(field)</h4> <p>Vary on <code>field</code>.</p> <h4 id="response-flushheaders-2"><a href="#response-flushheaders-2" aria-hidden="true" class="header-anchor">#</a> response.flushHeaders()</h4> <p>Flush any set headers, and begin the body.</p> <h2 id="advanced-responses"><a href="#advanced-responses" aria-hidden="true" class="header-anchor">#</a> Advanced responses</h2> <p>Strapi integrates <a href="https://github.com/hapijs/boom" target="_blank" rel="noopener noreferrer">Boom<svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a>: a set of utilities for returning HTTP errors. Every Booms functions are accessible through the <code>ctx.response</code>.</p> <p>You can also override responses based on them status. Please read the <a href="/documentation/3.x.x/configurations/configurations.html#responses">configuration responses</a> for that.</p> <div class="note custom-block"><p>Every Boom's functions is delegated to the context. It means that <code>ctx.notFound</code> is a shortcut to <code>ctx.response.notFound</code>.</p></div> <h3 id="api-reference-2"><a href="#api-reference-2" aria-hidden="true" class="header-anchor">#</a> API Reference</h3> <p>For more information, please refer to the <a href="https://github.com/hapijs/boom" target="_blank" rel="noopener noreferrer">Boom documentation<svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a>.</p> <ul><li><a href="#http-4xx-errors">HTTP 4xx Errors</a> <ul><li><a href="#ctxresponsebadrequestmessage-data"><code>ctx.response.badRequest([message], [data])</code></a></li> <li><a href="#ctxresponseunauthorizedmessage-scheme-attributes"><code>ctx.response.unauthorized([message], [scheme], [attributes])</code></a></li> <li><a href="#ctxresponsepaymentrequiredmessage-data"><code>ctx.response.paymentRequired([message], [data])</code></a></li> <li><a href="#ctxresponseforbiddenmessage-data"><code>ctx.response.forbidden([message], [data])</code></a></li> <li><a href="#ctxresponsenotfoundmessage-data"><code>ctx.response.notFound([message], [data])</code></a></li> <li><a href="#ctxresponsemethodnotallowedmessage-data-allow"><code>ctx.response.methodNotAllowed([message], [data], [allow])</code></a></li> <li><a href="#ctxresponsenotacceptablemessage-data"><code>ctx.response.notAcceptable([message], [data])</code></a></li> <li><a href="#ctxresponseproxyauthrequiredmessage-data"><code>ctx.response.proxyAuthRequired([message], [data])</code></a></li> <li><a href="#ctxresponseclienttimeoutmessage-data"><code>ctx.response.clientTimeout([message], [data])</code></a></li> <li><a href="#ctxresponseconflictmessage-data"><code>ctx.response.conflict([message], [data])</code></a></li> <li><a href="#ctxresponseresourcegonemessage-data"><code>ctx.response.resourceGone([message], [data])</code></a></li> <li><a href="#ctxresponselengthrequiredmessage-data"><code>ctx.response.lengthRequired([message], [data])</code></a></li> <li><a href="#ctxresponsepreconditionfailedmessage-data"><code>ctx.response.preconditionFailed([message], [data])</code></a></li> <li><a href="#ctxresponseentitytoolargemessage-data"><code>ctx.response.entityTooLarge([message], [data])</code></a></li> <li><a href="#ctxresponseuritoolongmessage-data"><code>ctx.response.uriTooLong([message], [data])</code></a></li> <li><a href="#ctxresponseunsupportedmediatypemessage-data"><code>ctx.response.unsupportedMediaType([message], [data])</code></a></li> <li><a href="#ctxresponserangenotsatisfiablemessage-data"><code>ctx.response.rangeNotSatisfiable([message], [data])</code></a></li> <li><a href="#ctxresponseexpectationfailedmessage-data"><code>ctx.response.expectationFailed([message], [data])</code></a></li> <li><a href="#ctxresponseteapotmessage-data"><code>ctx.response.teapot([message], [data])</code></a></li> <li><a href="#ctxresponsebaddatamessage-data"><code>ctx.response.badData([message], [data])</code></a></li> <li><a href="#ctxresponselockedmessage-data"><code>ctx.response.locked([message], [data])</code></a></li> <li><a href="#ctxresponsepreconditionrequiredmessage-data"><code>ctx.response.preconditionRequired([message], [data])</code></a></li> <li><a href="#ctxresponsetoomanyrequestsmessage-data"><code>ctx.response.tooManyRequests([message], [data])</code></a></li> <li><a href="#ctxresponseillegalmessage-data"><code>ctx.response.illegal([message], [data])</code></a></li></ul></li> <li><a href="#http-5xx-errors">HTTP 5xx Errors</a> <ul><li><a href="#ctxresponsebadimplementationmessage-data---alias-internal"><code>ctx.response.badImplementation([message], [data])</code> - (<em>alias: <code>internal</code></em>)</a></li> <li><a href="#ctxresponsenotimplementedmessage-data"><code>ctx.response.notImplemented([message], [data])</code></a></li> <li><a href="#ctxresponsebadgatewaymessage-data"><code>ctx.response.badGateway([message], [data])</code></a></li> <li><a href="#ctxresponseserverunavailablemessage-data"><code>ctx.response.serverUnavailable([message], [data])</code></a></li> <li><a href="#ctxresponsegatewaytimeoutmessage-data"><code>ctx.response.gatewayTimeout([message], [data])</code></a></li></ul></li></ul> <h3 id="http-4xx-errors"><a href="#http-4xx-errors" aria-hidden="true" class="header-anchor">#</a> HTTP 4xx Errors</h3> <h4 id="ctx-response-badrequest-message-data"><a href="#ctx-response-badrequest-message-data" aria-hidden="true" class="header-anchor">#</a> <code>ctx.response.badRequest([message], [data])</code></h4> <p>Returns a 400 Bad Request error where:</p> <ul><li><code>message</code> - optional message.</li> <li><code>data</code> - optional additional error data.</li></ul> <div class="language-js extra-class"><pre class="language-js"><code>ctx<span class="token punctuation">.</span>response<span class="token punctuation">.</span><span class="token function">badRequest</span><span class="token punctuation">(</span><span class="token string">'invalid query'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre></div><p>Generates the following response payload:</p> <div class="language-json extra-class"><pre class="language-json"><code><span class="token punctuation">{</span>
<span class="token property">&quot;statusCode&quot;</span><span class="token operator">:</span> <span class="token number">400</span><span class="token punctuation">,</span>
<span class="token property">&quot;error&quot;</span><span class="token operator">:</span> <span class="token string">&quot;Bad Request&quot;</span><span class="token punctuation">,</span>
<span class="token property">&quot;message&quot;</span><span class="token operator">:</span> <span class="token string">&quot;invalid query&quot;</span>
<span class="token punctuation">}</span>
</code></pre></div><h4 id="ctx-response-unauthorized-message-scheme-attributes"><a href="#ctx-response-unauthorized-message-scheme-attributes" aria-hidden="true" class="header-anchor">#</a> <code>ctx.response.unauthorized([message], [scheme], [attributes])</code></h4> <p>Returns a 401 Unauthorized error where:</p> <ul><li><code>message</code> - optional message.</li> <li><code>scheme</code> can be one of the following:
<ul><li>an authentication scheme name</li> <li>an array of string values. These values will be separated by ', ' and set to the 'WWW-Authenticate' header.</li></ul></li> <li><code>attributes</code> - an object of values to use while setting the 'WWW-Authenticate' header. This value is only used
when <code>scheme</code> is a string, otherwise it is ignored. Every key/value pair will be included in the
'WWW-Authenticate' in the format of 'key=&quot;value&quot;' as well as in the response payload under the <code>attributes</code> key. Alternatively value can be a string which is use to set the value of the scheme, for example setting the token value for negotiate header. If string is used message parameter must be null.
<code>null</code> and <code>undefined</code> will be replaced with an empty string. If <code>attributes</code> is set, <code>message</code> will be used as
the 'error' segment of the 'WWW-Authenticate' header. If <code>message</code> is unset, the 'error' segment of the header
will not be present and <code>isMissing</code> will be true on the error object.</li></ul> <p>If either <code>scheme</code> or <code>attributes</code> are set, the resultant <code>Boom</code> object will have the 'WWW-Authenticate' header set for the response.</p> <div class="language-js extra-class"><pre class="language-js"><code>ctx<span class="token punctuation">.</span>response<span class="token punctuation">.</span><span class="token function">unauthorized</span><span class="token punctuation">(</span><span class="token string">'invalid password'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre></div><p>Generates the following response:</p> <div class="language-json extra-class"><pre class="language-json"><code><span class="token property">&quot;payload&quot;</span><span class="token operator">:</span> <span class="token punctuation">{</span>
<span class="token property">&quot;statusCode&quot;</span><span class="token operator">:</span> <span class="token number">401</span><span class="token punctuation">,</span>
<span class="token property">&quot;error&quot;</span><span class="token operator">:</span> <span class="token string">&quot;Unauthorized&quot;</span><span class="token punctuation">,</span>
<span class="token property">&quot;message&quot;</span><span class="token operator">:</span> <span class="token string">&quot;invalid password&quot;</span>
<span class="token punctuation">}</span><span class="token punctuation">,</span>
<span class="token string">&quot;headers&quot;</span> <span class="token punctuation">{</span><span class="token punctuation">}</span>
</code></pre></div><div class="language-js extra-class"><pre class="language-js"><code>ctx<span class="token punctuation">.</span>response<span class="token punctuation">.</span><span class="token function">unauthorized</span><span class="token punctuation">(</span><span class="token string">'invalid password'</span><span class="token punctuation">,</span> <span class="token string">'sample'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre></div><p>Generates the following response:</p> <div class="language-json extra-class"><pre class="language-json"><code><span class="token property">&quot;payload&quot;</span><span class="token operator">:</span> <span class="token punctuation">{</span>
<span class="token property">&quot;statusCode&quot;</span><span class="token operator">:</span> <span class="token number">401</span><span class="token punctuation">,</span>
<span class="token property">&quot;error&quot;</span><span class="token operator">:</span> <span class="token string">&quot;Unauthorized&quot;</span><span class="token punctuation">,</span>
<span class="token property">&quot;message&quot;</span><span class="token operator">:</span> <span class="token string">&quot;invalid password&quot;</span><span class="token punctuation">,</span>
<span class="token property">&quot;attributes&quot;</span><span class="token operator">:</span> <span class="token punctuation">{</span>
<span class="token property">&quot;error&quot;</span><span class="token operator">:</span> <span class="token string">&quot;invalid password&quot;</span>
<span class="token punctuation">}</span>
<span class="token punctuation">}</span><span class="token punctuation">,</span>
<span class="token string">&quot;headers&quot;</span> <span class="token punctuation">{</span>
<span class="token property">&quot;WWW-Authenticate&quot;</span><span class="token operator">:</span> <span class="token string">&quot;sample error=\&quot;invalid password\&quot;&quot;</span>
<span class="token punctuation">}</span>
</code></pre></div><div class="language-js extra-class"><pre class="language-js"><code>ctx<span class="token punctuation">.</span>response<span class="token punctuation">.</span><span class="token function">unauthorized</span><span class="token punctuation">(</span><span class="token keyword">null</span><span class="token punctuation">,</span> <span class="token string">'Negotiate'</span><span class="token punctuation">,</span> <span class="token string">'VGhpcyBpcyBhIHRlc3QgdG9rZW4='</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre></div><p>Generates the following response:</p> <div class="language-json extra-class"><pre class="language-json"><code><span class="token property">&quot;payload&quot;</span><span class="token operator">:</span> <span class="token punctuation">{</span>
<span class="token property">&quot;statusCode&quot;</span><span class="token operator">:</span> <span class="token number">401</span><span class="token punctuation">,</span>
<span class="token property">&quot;error&quot;</span><span class="token operator">:</span> <span class="token string">&quot;Unauthorized&quot;</span><span class="token punctuation">,</span>
<span class="token property">&quot;attributes&quot;</span><span class="token operator">:</span> <span class="token string">&quot;VGhpcyBpcyBhIHRlc3QgdG9rZW4=&quot;</span>
<span class="token punctuation">}</span><span class="token punctuation">,</span>
<span class="token string">&quot;headers&quot;</span> <span class="token punctuation">{</span>
<span class="token property">&quot;WWW-Authenticate&quot;</span><span class="token operator">:</span> <span class="token string">&quot;Negotiate VGhpcyBpcyBhIHRlc3QgdG9rZW4=&quot;</span>
<span class="token punctuation">}</span>
</code></pre></div><div class="language-js extra-class"><pre class="language-js"><code>ctx<span class="token punctuation">.</span>response<span class="token punctuation">.</span><span class="token function">unauthorized</span><span class="token punctuation">(</span><span class="token string">'invalid password'</span><span class="token punctuation">,</span> <span class="token string">'sample'</span><span class="token punctuation">,</span> <span class="token punctuation">{</span> ttl<span class="token punctuation">:</span> <span class="token number">0</span><span class="token punctuation">,</span> cache<span class="token punctuation">:</span> <span class="token keyword">null</span><span class="token punctuation">,</span> foo<span class="token punctuation">:</span> <span class="token string">'bar'</span> <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre></div><p>Generates the following response:</p> <div class="language-json extra-class"><pre class="language-json"><code><span class="token property">&quot;payload&quot;</span><span class="token operator">:</span> <span class="token punctuation">{</span>
<span class="token property">&quot;statusCode&quot;</span><span class="token operator">:</span> <span class="token number">401</span><span class="token punctuation">,</span>
<span class="token property">&quot;error&quot;</span><span class="token operator">:</span> <span class="token string">&quot;Unauthorized&quot;</span><span class="token punctuation">,</span>
<span class="token property">&quot;message&quot;</span><span class="token operator">:</span> <span class="token string">&quot;invalid password&quot;</span><span class="token punctuation">,</span>
<span class="token property">&quot;attributes&quot;</span><span class="token operator">:</span> <span class="token punctuation">{</span>
<span class="token property">&quot;error&quot;</span><span class="token operator">:</span> <span class="token string">&quot;invalid password&quot;</span><span class="token punctuation">,</span>
<span class="token property">&quot;ttl&quot;</span><span class="token operator">:</span> <span class="token number">0</span><span class="token punctuation">,</span>
<span class="token property">&quot;cache&quot;</span><span class="token operator">:</span> <span class="token string">&quot;&quot;</span><span class="token punctuation">,</span>
<span class="token property">&quot;foo&quot;</span><span class="token operator">:</span> <span class="token string">&quot;bar&quot;</span>
<span class="token punctuation">}</span>
<span class="token punctuation">}</span><span class="token punctuation">,</span>
<span class="token string">&quot;headers&quot;</span> <span class="token punctuation">{</span>
<span class="token property">&quot;WWW-Authenticate&quot;</span><span class="token operator">:</span> <span class="token string">&quot;sample ttl=\&quot;0\&quot;, cache=\&quot;\&quot;, foo=\&quot;bar\&quot;, error=\&quot;invalid password\&quot;&quot;</span>
<span class="token punctuation">}</span>
</code></pre></div><h4 id="ctx-response-paymentrequired-message-data"><a href="#ctx-response-paymentrequired-message-data" aria-hidden="true" class="header-anchor">#</a> <code>ctx.response.paymentRequired([message], [data])</code></h4> <p>Returns a 402 Payment Required error where:</p> <ul><li><code>message</code> - optional message.</li> <li><code>data</code> - optional additional error data.</li></ul> <div class="language-js extra-class"><pre class="language-js"><code>ctx<span class="token punctuation">.</span>response<span class="token punctuation">.</span><span class="token function">paymentRequired</span><span class="token punctuation">(</span><span class="token string">'bandwidth used'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre></div><p>Generates the following response payload:</p> <div class="language-json extra-class"><pre class="language-json"><code><span class="token punctuation">{</span>
<span class="token property">&quot;statusCode&quot;</span><span class="token operator">:</span> <span class="token number">402</span><span class="token punctuation">,</span>
<span class="token property">&quot;error&quot;</span><span class="token operator">:</span> <span class="token string">&quot;Payment Required&quot;</span><span class="token punctuation">,</span>
<span class="token property">&quot;message&quot;</span><span class="token operator">:</span> <span class="token string">&quot;bandwidth used&quot;</span>
<span class="token punctuation">}</span>
</code></pre></div><h4 id="ctx-response-forbidden-message-data"><a href="#ctx-response-forbidden-message-data" aria-hidden="true" class="header-anchor">#</a> <code>ctx.response.forbidden([message], [data])</code></h4> <p>Returns a 403 Forbidden error where:</p> <ul><li><code>message</code> - optional message.</li> <li><code>data</code> - optional additional error data.</li></ul> <div class="language-js extra-class"><pre class="language-js"><code>ctx<span class="token punctuation">.</span>response<span class="token punctuation">.</span><span class="token function">forbidden</span><span class="token punctuation">(</span><span class="token string">'try again some time'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre></div><p>Generates the following response payload:</p> <div class="language-json extra-class"><pre class="language-json"><code><span class="token punctuation">{</span>
<span class="token property">&quot;statusCode&quot;</span><span class="token operator">:</span> <span class="token number">403</span><span class="token punctuation">,</span>
<span class="token property">&quot;error&quot;</span><span class="token operator">:</span> <span class="token string">&quot;Forbidden&quot;</span><span class="token punctuation">,</span>
<span class="token property">&quot;message&quot;</span><span class="token operator">:</span> <span class="token string">&quot;try again some time&quot;</span>
<span class="token punctuation">}</span>
</code></pre></div><h4 id="ctx-response-notfound-message-data"><a href="#ctx-response-notfound-message-data" aria-hidden="true" class="header-anchor">#</a> <code>ctx.response.notFound([message], [data])</code></h4> <p>Returns a 404 Not Found error where:</p> <ul><li><code>message</code> - optional message.</li> <li><code>data</code> - optional additional error data.</li></ul> <div class="language-js extra-class"><pre class="language-js"><code>ctx<span class="token punctuation">.</span>response<span class="token punctuation">.</span><span class="token function">notFound</span><span class="token punctuation">(</span><span class="token string">'missing'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre></div><p>Generates the following response payload:</p> <div class="language-json extra-class"><pre class="language-json"><code><span class="token punctuation">{</span>
<span class="token property">&quot;statusCode&quot;</span><span class="token operator">:</span> <span class="token number">404</span><span class="token punctuation">,</span>
<span class="token property">&quot;error&quot;</span><span class="token operator">:</span> <span class="token string">&quot;Not Found&quot;</span><span class="token punctuation">,</span>
<span class="token property">&quot;message&quot;</span><span class="token operator">:</span> <span class="token string">&quot;missing&quot;</span>
<span class="token punctuation">}</span>
</code></pre></div><h4 id="ctx-response-methodnotallowed-message-data-allow"><a href="#ctx-response-methodnotallowed-message-data-allow" aria-hidden="true" class="header-anchor">#</a> <code>ctx.response.methodNotAllowed([message], [data], [allow])</code></h4> <p>Returns a 405 Method Not Allowed error where:</p> <ul><li><code>message</code> - optional message.</li> <li><code>data</code> - optional additional error data.</li> <li><code>allow</code> - optional string or array of strings (to be combined and separated by ', ') which is set to the 'Allow' header.</li></ul> <div class="language-js extra-class"><pre class="language-js"><code>ctx<span class="token punctuation">.</span>response<span class="token punctuation">.</span><span class="token function">methodNotAllowed</span><span class="token punctuation">(</span><span class="token string">'that method is not allowed'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre></div><p>Generates the following response payload:</p> <div class="language-json extra-class"><pre class="language-json"><code><span class="token punctuation">{</span>
<span class="token property">&quot;statusCode&quot;</span><span class="token operator">:</span> <span class="token number">405</span><span class="token punctuation">,</span>
<span class="token property">&quot;error&quot;</span><span class="token operator">:</span> <span class="token string">&quot;Method Not Allowed&quot;</span><span class="token punctuation">,</span>
<span class="token property">&quot;message&quot;</span><span class="token operator">:</span> <span class="token string">&quot;that method is not allowed&quot;</span>
<span class="token punctuation">}</span>
</code></pre></div><h4 id="ctx-response-notacceptable-message-data"><a href="#ctx-response-notacceptable-message-data" aria-hidden="true" class="header-anchor">#</a> <code>ctx.response.notAcceptable([message], [data])</code></h4> <p>Returns a 406 Not Acceptable error where:</p> <ul><li><code>message</code> - optional message.</li> <li><code>data</code> - optional additional error data.</li></ul> <div class="language-js extra-class"><pre class="language-js"><code>ctx<span class="token punctuation">.</span>response<span class="token punctuation">.</span><span class="token function">notAcceptable</span><span class="token punctuation">(</span><span class="token string">'unacceptable'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre></div><p>Generates the following response payload:</p> <div class="language-json extra-class"><pre class="language-json"><code><span class="token punctuation">{</span>
<span class="token property">&quot;statusCode&quot;</span><span class="token operator">:</span> <span class="token number">406</span><span class="token punctuation">,</span>
<span class="token property">&quot;error&quot;</span><span class="token operator">:</span> <span class="token string">&quot;Not Acceptable&quot;</span><span class="token punctuation">,</span>
<span class="token property">&quot;message&quot;</span><span class="token operator">:</span> <span class="token string">&quot;unacceptable&quot;</span>
<span class="token punctuation">}</span>
</code></pre></div><h4 id="ctx-response-proxyauthrequired-message-data"><a href="#ctx-response-proxyauthrequired-message-data" aria-hidden="true" class="header-anchor">#</a> <code>ctx.response.proxyAuthRequired([message], [data])</code></h4> <p>Returns a 407 Proxy Authentication Required error where:</p> <ul><li><code>message</code> - optional message.</li> <li><code>data</code> - optional additional error data.</li></ul> <div class="language-js extra-class"><pre class="language-js"><code>ctx<span class="token punctuation">.</span>response<span class="token punctuation">.</span><span class="token function">proxyAuthRequired</span><span class="token punctuation">(</span><span class="token string">'auth missing'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre></div><p>Generates the following response payload:</p> <div class="language-json extra-class"><pre class="language-json"><code><span class="token punctuation">{</span>
<span class="token property">&quot;statusCode&quot;</span><span class="token operator">:</span> <span class="token number">407</span><span class="token punctuation">,</span>
<span class="token property">&quot;error&quot;</span><span class="token operator">:</span> <span class="token string">&quot;Proxy Authentication Required&quot;</span><span class="token punctuation">,</span>
<span class="token property">&quot;message&quot;</span><span class="token operator">:</span> <span class="token string">&quot;auth missing&quot;</span>
<span class="token punctuation">}</span>
</code></pre></div><h4 id="ctx-response-clienttimeout-message-data"><a href="#ctx-response-clienttimeout-message-data" aria-hidden="true" class="header-anchor">#</a> <code>ctx.response.clientTimeout([message], [data])</code></h4> <p>Returns a 408 Request Time-out error where:</p> <ul><li><code>message</code> - optional message.</li> <li><code>data</code> - optional additional error data.</li></ul> <div class="language-js extra-class"><pre class="language-js"><code>ctx<span class="token punctuation">.</span>response<span class="token punctuation">.</span><span class="token function">clientTimeout</span><span class="token punctuation">(</span><span class="token string">'timed out'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre></div><p>Generates the following response payload:</p> <div class="language-json extra-class"><pre class="language-json"><code><span class="token punctuation">{</span>
<span class="token property">&quot;statusCode&quot;</span><span class="token operator">:</span> <span class="token number">408</span><span class="token punctuation">,</span>
<span class="token property">&quot;error&quot;</span><span class="token operator">:</span> <span class="token string">&quot;Request Time-out&quot;</span><span class="token punctuation">,</span>
<span class="token property">&quot;message&quot;</span><span class="token operator">:</span> <span class="token string">&quot;timed out&quot;</span>
<span class="token punctuation">}</span>
</code></pre></div><h4 id="ctx-response-conflict-message-data"><a href="#ctx-response-conflict-message-data" aria-hidden="true" class="header-anchor">#</a> <code>ctx.response.conflict([message], [data])</code></h4> <p>Returns a 409 Conflict error where:</p> <ul><li><code>message</code> - optional message.</li> <li><code>data</code> - optional additional error data.</li></ul> <div class="language-js extra-class"><pre class="language-js"><code>ctx<span class="token punctuation">.</span>response<span class="token punctuation">.</span><span class="token function">conflict</span><span class="token punctuation">(</span><span class="token string">'there was a conflict'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre></div><p>Generates the following response payload:</p> <div class="language-json extra-class"><pre class="language-json"><code><span class="token punctuation">{</span>
<span class="token property">&quot;statusCode&quot;</span><span class="token operator">:</span> <span class="token number">409</span><span class="token punctuation">,</span>
<span class="token property">&quot;error&quot;</span><span class="token operator">:</span> <span class="token string">&quot;Conflict&quot;</span><span class="token punctuation">,</span>
<span class="token property">&quot;message&quot;</span><span class="token operator">:</span> <span class="token string">&quot;there was a conflict&quot;</span>
<span class="token punctuation">}</span>
</code></pre></div><h4 id="ctx-response-resourcegone-message-data"><a href="#ctx-response-resourcegone-message-data" aria-hidden="true" class="header-anchor">#</a> <code>ctx.response.resourceGone([message], [data])</code></h4> <p>Returns a 410 Gone error where:</p> <ul><li><code>message</code> - optional message.</li> <li><code>data</code> - optional additional error data.</li></ul> <div class="language-js extra-class"><pre class="language-js"><code>ctx<span class="token punctuation">.</span>response<span class="token punctuation">.</span><span class="token function">resourceGone</span><span class="token punctuation">(</span><span class="token string">'it is gone'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre></div><p>Generates the following response payload:</p> <div class="language-json extra-class"><pre class="language-json"><code><span class="token punctuation">{</span>
<span class="token property">&quot;statusCode&quot;</span><span class="token operator">:</span> <span class="token number">410</span><span class="token punctuation">,</span>
<span class="token property">&quot;error&quot;</span><span class="token operator">:</span> <span class="token string">&quot;Gone&quot;</span><span class="token punctuation">,</span>
<span class="token property">&quot;message&quot;</span><span class="token operator">:</span> <span class="token string">&quot;it is gone&quot;</span>
<span class="token punctuation">}</span>
</code></pre></div><h4 id="ctx-response-lengthrequired-message-data"><a href="#ctx-response-lengthrequired-message-data" aria-hidden="true" class="header-anchor">#</a> <code>ctx.response.lengthRequired([message], [data])</code></h4> <p>Returns a 411 Length Required error where:</p> <ul><li><code>message</code> - optional message.</li> <li><code>data</code> - optional additional error data.</li></ul> <div class="language-js extra-class"><pre class="language-js"><code>ctx<span class="token punctuation">.</span>response<span class="token punctuation">.</span><span class="token function">lengthRequired</span><span class="token punctuation">(</span><span class="token string">'length needed'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre></div><p>Generates the following response payload:</p> <div class="language-json extra-class"><pre class="language-json"><code><span class="token punctuation">{</span>
<span class="token property">&quot;statusCode&quot;</span><span class="token operator">:</span> <span class="token number">411</span><span class="token punctuation">,</span>
<span class="token property">&quot;error&quot;</span><span class="token operator">:</span> <span class="token string">&quot;Length Required&quot;</span><span class="token punctuation">,</span>
<span class="token property">&quot;message&quot;</span><span class="token operator">:</span> <span class="token string">&quot;length needed&quot;</span>
<span class="token punctuation">}</span>
</code></pre></div><h4 id="ctx-response-preconditionfailed-message-data"><a href="#ctx-response-preconditionfailed-message-data" aria-hidden="true" class="header-anchor">#</a> <code>ctx.response.preconditionFailed([message], [data])</code></h4> <p>Returns a 412 Precondition Failed error where:</p> <ul><li><code>message</code> - optional message.</li> <li><code>data</code> - optional additional error data.</li></ul> <div class="language-js extra-class"><pre class="language-js"><code>ctx<span class="token punctuation">.</span>response<span class="token punctuation">.</span><span class="token function">preconditionFailed</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre></div><p>Generates the following response payload:</p> <div class="language-json extra-class"><pre class="language-json"><code><span class="token punctuation">{</span>
<span class="token property">&quot;statusCode&quot;</span><span class="token operator">:</span> <span class="token number">412</span><span class="token punctuation">,</span>
<span class="token property">&quot;error&quot;</span><span class="token operator">:</span> <span class="token string">&quot;Precondition Failed&quot;</span>
<span class="token punctuation">}</span>
</code></pre></div><h4 id="ctx-response-entitytoolarge-message-data"><a href="#ctx-response-entitytoolarge-message-data" aria-hidden="true" class="header-anchor">#</a> <code>ctx.response.entityTooLarge([message], [data])</code></h4> <p>Returns a 413 Request Entity Too Large error where:</p> <ul><li><code>message</code> - optional message.</li> <li><code>data</code> - optional additional error data.</li></ul> <div class="language-js extra-class"><pre class="language-js"><code>ctx<span class="token punctuation">.</span>response<span class="token punctuation">.</span><span class="token function">entityTooLarge</span><span class="token punctuation">(</span><span class="token string">'too big'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre></div><p>Generates the following response payload:</p> <div class="language-json extra-class"><pre class="language-json"><code><span class="token punctuation">{</span>
<span class="token property">&quot;statusCode&quot;</span><span class="token operator">:</span> <span class="token number">413</span><span class="token punctuation">,</span>
<span class="token property">&quot;error&quot;</span><span class="token operator">:</span> <span class="token string">&quot;Request Entity Too Large&quot;</span><span class="token punctuation">,</span>
<span class="token property">&quot;message&quot;</span><span class="token operator">:</span> <span class="token string">&quot;too big&quot;</span>
<span class="token punctuation">}</span>
</code></pre></div><h4 id="ctx-response-uritoolong-message-data"><a href="#ctx-response-uritoolong-message-data" aria-hidden="true" class="header-anchor">#</a> <code>ctx.response.uriTooLong([message], [data])</code></h4> <p>Returns a 414 Request-URI Too Large error where:</p> <ul><li><code>message</code> - optional message.</li> <li><code>data</code> - optional additional error data.</li></ul> <div class="language-js extra-class"><pre class="language-js"><code>ctx<span class="token punctuation">.</span>response<span class="token punctuation">.</span><span class="token function">uriTooLong</span><span class="token punctuation">(</span><span class="token string">'uri is too long'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre></div><p>Generates the following response payload:</p> <div class="language-json extra-class"><pre class="language-json"><code><span class="token punctuation">{</span>
<span class="token property">&quot;statusCode&quot;</span><span class="token operator">:</span> <span class="token number">414</span><span class="token punctuation">,</span>
<span class="token property">&quot;error&quot;</span><span class="token operator">:</span> <span class="token string">&quot;Request-URI Too Large&quot;</span><span class="token punctuation">,</span>
<span class="token property">&quot;message&quot;</span><span class="token operator">:</span> <span class="token string">&quot;uri is too long&quot;</span>
<span class="token punctuation">}</span>
</code></pre></div><h4 id="ctx-response-unsupportedmediatype-message-data"><a href="#ctx-response-unsupportedmediatype-message-data" aria-hidden="true" class="header-anchor">#</a> <code>ctx.response.unsupportedMediaType([message], [data])</code></h4> <p>Returns a 415 Unsupported Media Type error where:</p> <ul><li><code>message</code> - optional message.</li> <li><code>data</code> - optional additional error data.</li></ul> <div class="language-js extra-class"><pre class="language-js"><code>ctx<span class="token punctuation">.</span>response<span class="token punctuation">.</span><span class="token function">unsupportedMediaType</span><span class="token punctuation">(</span><span class="token string">'that media is not supported'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre></div><p>Generates the following response payload:</p> <div class="language-json extra-class"><pre class="language-json"><code><span class="token punctuation">{</span>
<span class="token property">&quot;statusCode&quot;</span><span class="token operator">:</span> <span class="token number">415</span><span class="token punctuation">,</span>
<span class="token property">&quot;error&quot;</span><span class="token operator">:</span> <span class="token string">&quot;Unsupported Media Type&quot;</span><span class="token punctuation">,</span>
<span class="token property">&quot;message&quot;</span><span class="token operator">:</span> <span class="token string">&quot;that media is not supported&quot;</span>
<span class="token punctuation">}</span>
</code></pre></div><h4 id="ctx-response-rangenotsatisfiable-message-data"><a href="#ctx-response-rangenotsatisfiable-message-data" aria-hidden="true" class="header-anchor">#</a> <code>ctx.response.rangeNotSatisfiable([message], [data])</code></h4> <p>Returns a 416 Requested Range Not Satisfiable error where:</p> <ul><li><code>message</code> - optional message.</li> <li><code>data</code> - optional additional error data.</li></ul> <div class="language-js extra-class"><pre class="language-js"><code>ctx<span class="token punctuation">.</span>response<span class="token punctuation">.</span><span class="token function">rangeNotSatisfiable</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre></div><p>Generates the following response payload:</p> <div class="language-json extra-class"><pre class="language-json"><code><span class="token punctuation">{</span>
<span class="token property">&quot;statusCode&quot;</span><span class="token operator">:</span> <span class="token number">416</span><span class="token punctuation">,</span>
<span class="token property">&quot;error&quot;</span><span class="token operator">:</span> <span class="token string">&quot;Requested Range Not Satisfiable&quot;</span>
<span class="token punctuation">}</span>
</code></pre></div><h4 id="ctx-response-expectationfailed-message-data"><a href="#ctx-response-expectationfailed-message-data" aria-hidden="true" class="header-anchor">#</a> <code>ctx.response.expectationFailed([message], [data])</code></h4> <p>Returns a 417 Expectation Failed error where:</p> <ul><li><code>message</code> - optional message.</li> <li><code>data</code> - optional additional error data.</li></ul> <div class="language-js extra-class"><pre class="language-js"><code>ctx<span class="token punctuation">.</span>response<span class="token punctuation">.</span><span class="token function">expectationFailed</span><span class="token punctuation">(</span><span class="token string">'expected this to work'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre></div><p>Generates the following response payload:</p> <div class="language-json extra-class"><pre class="language-json"><code><span class="token punctuation">{</span>
<span class="token property">&quot;statusCode&quot;</span><span class="token operator">:</span> <span class="token number">417</span><span class="token punctuation">,</span>
<span class="token property">&quot;error&quot;</span><span class="token operator">:</span> <span class="token string">&quot;Expectation Failed&quot;</span><span class="token punctuation">,</span>
<span class="token property">&quot;message&quot;</span><span class="token operator">:</span> <span class="token string">&quot;expected this to work&quot;</span>
<span class="token punctuation">}</span>
</code></pre></div><h4 id="ctx-response-teapot-message-data"><a href="#ctx-response-teapot-message-data" aria-hidden="true" class="header-anchor">#</a> <code>ctx.response.teapot([message], [data])</code></h4> <p>Returns a 418 I'm a Teapot error where:</p> <ul><li><code>message</code> - optional message.</li> <li><code>data</code> - optional additional error data.</li></ul> <div class="language-js extra-class"><pre class="language-js"><code>ctx<span class="token punctuation">.</span>response<span class="token punctuation">.</span><span class="token function">teapot</span><span class="token punctuation">(</span><span class="token string">'sorry, no coffee...'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre></div><p>Generates the following response payload:</p> <div class="language-json extra-class"><pre class="language-json"><code><span class="token punctuation">{</span>
<span class="token property">&quot;statusCode&quot;</span><span class="token operator">:</span> <span class="token number">418</span><span class="token punctuation">,</span>
<span class="token property">&quot;error&quot;</span><span class="token operator">:</span> <span class="token string">&quot;I'm a Teapot&quot;</span><span class="token punctuation">,</span>
<span class="token property">&quot;message&quot;</span><span class="token operator">:</span> <span class="token string">&quot;Sorry, no coffee...&quot;</span>
<span class="token punctuation">}</span>
</code></pre></div><h4 id="ctx-response-baddata-message-data"><a href="#ctx-response-baddata-message-data" aria-hidden="true" class="header-anchor">#</a> <code>ctx.response.badData([message], [data])</code></h4> <p>Returns a 422 Unprocessable Entity error where:</p> <ul><li><code>message</code> - optional message.</li> <li><code>data</code> - optional additional error data.</li></ul> <div class="language-js extra-class"><pre class="language-js"><code>ctx<span class="token punctuation">.</span>response<span class="token punctuation">.</span><span class="token function">badData</span><span class="token punctuation">(</span><span class="token string">'your data is bad and you should feel bad'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre></div><p>Generates the following response payload:</p> <div class="language-json extra-class"><pre class="language-json"><code><span class="token punctuation">{</span>
<span class="token property">&quot;statusCode&quot;</span><span class="token operator">:</span> <span class="token number">422</span><span class="token punctuation">,</span>
<span class="token property">&quot;error&quot;</span><span class="token operator">:</span> <span class="token string">&quot;Unprocessable Entity&quot;</span><span class="token punctuation">,</span>
<span class="token property">&quot;message&quot;</span><span class="token operator">:</span> <span class="token string">&quot;your data is bad and you should feel bad&quot;</span>
<span class="token punctuation">}</span>
</code></pre></div><h4 id="ctx-response-locked-message-data"><a href="#ctx-response-locked-message-data" aria-hidden="true" class="header-anchor">#</a> <code>ctx.response.locked([message], [data])</code></h4> <p>Returns a 423 Locked error where:</p> <ul><li><code>message</code> - optional message.</li> <li><code>data</code> - optional additional error data.</li></ul> <div class="language-js extra-class"><pre class="language-js"><code>ctx<span class="token punctuation">.</span>response<span class="token punctuation">.</span><span class="token function">locked</span><span class="token punctuation">(</span><span class="token string">'this resource has been locked'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre></div><p>Generates the following response payload:</p> <div class="language-json extra-class"><pre class="language-json"><code><span class="token punctuation">{</span>
<span class="token property">&quot;statusCode&quot;</span><span class="token operator">:</span> <span class="token number">423</span><span class="token punctuation">,</span>
<span class="token property">&quot;error&quot;</span><span class="token operator">:</span> <span class="token string">&quot;Locked&quot;</span><span class="token punctuation">,</span>
<span class="token property">&quot;message&quot;</span><span class="token operator">:</span> <span class="token string">&quot;this resource has been locked&quot;</span>
<span class="token punctuation">}</span>
</code></pre></div><h4 id="ctx-response-preconditionrequired-message-data"><a href="#ctx-response-preconditionrequired-message-data" aria-hidden="true" class="header-anchor">#</a> <code>ctx.response.preconditionRequired([message], [data])</code></h4> <p>Returns a 428 Precondition Required error where:</p> <ul><li><code>message</code> - optional message.</li> <li><code>data</code> - optional additional error data.</li></ul> <div class="language-js extra-class"><pre class="language-js"><code>ctx<span class="token punctuation">.</span>response<span class="token punctuation">.</span><span class="token function">preconditionRequired</span><span class="token punctuation">(</span><span class="token string">'you must supply an If-Match header'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre></div><p>Generates the following response payload:</p> <div class="language-json extra-class"><pre class="language-json"><code><span class="token punctuation">{</span>
<span class="token property">&quot;statusCode&quot;</span><span class="token operator">:</span> <span class="token number">428</span><span class="token punctuation">,</span>
<span class="token property">&quot;error&quot;</span><span class="token operator">:</span> <span class="token string">&quot;Precondition Required&quot;</span><span class="token punctuation">,</span>
<span class="token property">&quot;message&quot;</span><span class="token operator">:</span> <span class="token string">&quot;you must supply an If-Match header&quot;</span>
<span class="token punctuation">}</span>
</code></pre></div><h4 id="ctx-response-toomanyrequests-message-data"><a href="#ctx-response-toomanyrequests-message-data" aria-hidden="true" class="header-anchor">#</a> <code>ctx.response.tooManyRequests([message], [data])</code></h4> <p>Returns a 429 Too Many Requests error where:</p> <ul><li><code>message</code> - optional message.</li> <li><code>data</code> - optional additional error data.</li></ul> <div class="language-js extra-class"><pre class="language-js"><code>ctx<span class="token punctuation">.</span>response<span class="token punctuation">.</span><span class="token function">tooManyRequests</span><span class="token punctuation">(</span><span class="token string">'you have exceeded your request limit'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre></div><p>Generates the following response payload:</p> <div class="language-json extra-class"><pre class="language-json"><code><span class="token punctuation">{</span>
<span class="token property">&quot;statusCode&quot;</span><span class="token operator">:</span> <span class="token number">429</span><span class="token punctuation">,</span>
<span class="token property">&quot;error&quot;</span><span class="token operator">:</span> <span class="token string">&quot;Too Many Requests&quot;</span><span class="token punctuation">,</span>
<span class="token property">&quot;message&quot;</span><span class="token operator">:</span> <span class="token string">&quot;you have exceeded your request limit&quot;</span>
<span class="token punctuation">}</span>
</code></pre></div><h4 id="ctx-response-illegal-message-data"><a href="#ctx-response-illegal-message-data" aria-hidden="true" class="header-anchor">#</a> <code>ctx.response.illegal([message], [data])</code></h4> <p>Returns a 451 Unavailable For Legal Reasons error where:</p> <ul><li><code>message</code> - optional message.</li> <li><code>data</code> - optional additional error data.</li></ul> <div class="language-js extra-class"><pre class="language-js"><code>ctx<span class="token punctuation">.</span>response<span class="token punctuation">.</span><span class="token function">illegal</span><span class="token punctuation">(</span><span class="token string">'you are not permitted to view this resource for legal reasons'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre></div><p>Generates the following response payload:</p> <div class="language-json extra-class"><pre class="language-json"><code><span class="token punctuation">{</span>
<span class="token property">&quot;statusCode&quot;</span><span class="token operator">:</span> <span class="token number">451</span><span class="token punctuation">,</span>
<span class="token property">&quot;error&quot;</span><span class="token operator">:</span> <span class="token string">&quot;Unavailable For Legal Reasons&quot;</span><span class="token punctuation">,</span>
<span class="token property">&quot;message&quot;</span><span class="token operator">:</span> <span class="token string">&quot;you are not permitted to view this resource for legal reasons&quot;</span>
<span class="token punctuation">}</span>
</code></pre></div><h3 id="http-5xx-errors"><a href="#http-5xx-errors" aria-hidden="true" class="header-anchor">#</a> HTTP 5xx Errors</h3> <p>All 500 errors hide your message from the end user. Your message is recorded in the server log.</p> <h4 id="ctx-response-badimplementation-message-data-alias-internal"><a href="#ctx-response-badimplementation-message-data-alias-internal" aria-hidden="true" class="header-anchor">#</a> <code>ctx.response.badImplementation([message], [data])</code> - (<em>alias: <code>internal</code></em>)</h4> <p>Returns a 500 Internal Server Error error where:</p> <ul><li><code>message</code> - optional message.</li> <li><code>data</code> - optional additional error data.</li></ul> <div class="language-js extra-class"><pre class="language-js"><code>ctx<span class="token punctuation">.</span>response<span class="token punctuation">.</span><span class="token function">badImplementation</span><span class="token punctuation">(</span><span class="token string">'terrible implementation'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre></div><p>Generates the following response payload:</p> <div class="language-json extra-class"><pre class="language-json"><code><span class="token punctuation">{</span>
<span class="token property">&quot;statusCode&quot;</span><span class="token operator">:</span> <span class="token number">500</span><span class="token punctuation">,</span>
<span class="token property">&quot;error&quot;</span><span class="token operator">:</span> <span class="token string">&quot;Internal Server Error&quot;</span><span class="token punctuation">,</span>
<span class="token property">&quot;message&quot;</span><span class="token operator">:</span> <span class="token string">&quot;An internal server error occurred&quot;</span>
<span class="token punctuation">}</span>
</code></pre></div><h4 id="ctx-response-notimplemented-message-data"><a href="#ctx-response-notimplemented-message-data" aria-hidden="true" class="header-anchor">#</a> <code>ctx.response.notImplemented([message], [data])</code></h4> <p>Returns a 501 Not Implemented error where:</p> <ul><li><code>message</code> - optional message.</li> <li><code>data</code> - optional additional error data.</li></ul> <div class="language-js extra-class"><pre class="language-js"><code>ctx<span class="token punctuation">.</span>response<span class="token punctuation">.</span><span class="token function">notImplemented</span><span class="token punctuation">(</span><span class="token string">'method not implemented'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre></div><p>Generates the following response payload:</p> <div class="language-json extra-class"><pre class="language-json"><code><span class="token punctuation">{</span>
<span class="token property">&quot;statusCode&quot;</span><span class="token operator">:</span> <span class="token number">501</span><span class="token punctuation">,</span>
<span class="token property">&quot;error&quot;</span><span class="token operator">:</span> <span class="token string">&quot;Not Implemented&quot;</span><span class="token punctuation">,</span>
<span class="token property">&quot;message&quot;</span><span class="token operator">:</span> <span class="token string">&quot;method not implemented&quot;</span>
<span class="token punctuation">}</span>
</code></pre></div><h4 id="ctx-response-badgateway-message-data"><a href="#ctx-response-badgateway-message-data" aria-hidden="true" class="header-anchor">#</a> <code>ctx.response.badGateway([message], [data])</code></h4> <p>Returns a 502 Bad Gateway error where:</p> <ul><li><code>message</code> - optional message.</li> <li><code>data</code> - optional additional error data.</li></ul> <div class="language-js extra-class"><pre class="language-js"><code>ctx<span class="token punctuation">.</span>response<span class="token punctuation">.</span><span class="token function">badGateway</span><span class="token punctuation">(</span><span class="token string">'that is a bad gateway'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre></div><p>Generates the following response payload:</p> <div class="language-json extra-class"><pre class="language-json"><code><span class="token punctuation">{</span>
<span class="token property">&quot;statusCode&quot;</span><span class="token operator">:</span> <span class="token number">502</span><span class="token punctuation">,</span>
<span class="token property">&quot;error&quot;</span><span class="token operator">:</span> <span class="token string">&quot;Bad Gateway&quot;</span><span class="token punctuation">,</span>
<span class="token property">&quot;message&quot;</span><span class="token operator">:</span> <span class="token string">&quot;that is a bad gateway&quot;</span>
<span class="token punctuation">}</span>
</code></pre></div><h4 id="ctx-response-serverunavailable-message-data"><a href="#ctx-response-serverunavailable-message-data" aria-hidden="true" class="header-anchor">#</a> <code>ctx.response.serverUnavailable([message], [data])</code></h4> <p>Returns a 503 Service Unavailable error where:</p> <ul><li><code>message</code> - optional message.</li> <li><code>data</code> - optional additional error data.</li></ul> <div class="language-js extra-class"><pre class="language-js"><code>ctx<span class="token punctuation">.</span>response<span class="token punctuation">.</span><span class="token function">serverUnavailable</span><span class="token punctuation">(</span><span class="token string">'unavailable'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre></div><p>Generates the following response payload:</p> <div class="language-json extra-class"><pre class="language-json"><code><span class="token punctuation">{</span>
<span class="token property">&quot;statusCode&quot;</span><span class="token operator">:</span> <span class="token number">503</span><span class="token punctuation">,</span>
<span class="token property">&quot;error&quot;</span><span class="token operator">:</span> <span class="token string">&quot;Service Unavailable&quot;</span><span class="token punctuation">,</span>
<span class="token property">&quot;message&quot;</span><span class="token operator">:</span> <span class="token string">&quot;unavailable&quot;</span>
<span class="token punctuation">}</span>
</code></pre></div><h4 id="ctx-response-gatewaytimeout-message-data"><a href="#ctx-response-gatewaytimeout-message-data" aria-hidden="true" class="header-anchor">#</a> <code>ctx.response.gatewayTimeout([message], [data])</code></h4> <p>Returns a 504 Gateway Time-out error where:</p> <ul><li><code>message</code> - optional message.</li> <li><code>data</code> - optional additional error data.</li></ul> <div class="language-js extra-class"><pre class="language-js"><code>ctx<span class="token punctuation">.</span>response<span class="token punctuation">.</span><span class="token function">gatewayTimeout</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre></div><p>Generates the following response payload:</p> <div class="language-json extra-class"><pre class="language-json"><code><span class="token punctuation">{</span>
<span class="token property">&quot;statusCode&quot;</span><span class="token operator">:</span> <span class="token number">504</span><span class="token punctuation">,</span>
<span class="token property">&quot;error&quot;</span><span class="token operator">:</span> <span class="token string">&quot;Gateway Time-out&quot;</span>
<span class="token punctuation">}</span>
</code></pre></div></div> <div class="page-edit"><div class="edit-link"><a href="https://github.com/strapi/strapi/edit/master/docs/3.x.x/guides/responses.md" target="_blank" rel="noopener noreferrer">Improve this page</a> <svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></div> <!----></div> <!----> </div></div></div>
<script src="/documentation/assets/js/51.b67aee1b.js" defer></script><script src="/documentation/assets/js/app.a8210063.js" defer></script>
</body>
</html>