mirror of
https://github.com/strapi/strapi.git
synced 2025-12-12 23:44:08 +00:00
366 lines
114 KiB
HTML
366 lines
114 KiB
HTML
<!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 "continue"</li> <li>101 "switching protocols"</li> <li>102 "processing"</li> <li>200 "ok"</li> <li>201 "created"</li> <li>202 "accepted"</li> <li>203 "non-authoritative information"</li> <li>204 "no content"</li> <li>205 "reset content"</li> <li>206 "partial content"</li> <li>207 "multi-status"</li> <li>208 "already reported"</li> <li>226 "im used"</li> <li>300 "multiple choices"</li> <li>301 "moved permanently"</li> <li>302 "found"</li> <li>303 "see other"</li> <li>304 "not modified"</li> <li>305 "use proxy"</li> <li>307 "temporary redirect"</li> <li>308 "permanent redirect"</li> <li>400 "bad request"</li> <li>401 "unauthorized"</li> <li>402 "payment required"</li> <li>403 "forbidden"</li> <li>404 "not found"</li> <li>405 "method not allowed"</li> <li>406 "not acceptable"</li> <li>407 "proxy authentication required"</li> <li>408 "request timeout"</li> <li>409 "conflict"</li> <li>410 "gone"</li> <li>411 "length required"</li> <li>412 "precondition failed"</li> <li>413 "payload too large"</li> <li>414 "uri too long"</li> <li>415 "unsupported media type"</li> <li>416 "range not satisfiable"</li> <li>417 "expectation failed"</li> <li>418 "I'm a teapot"</li> <li>422 "unprocessable entity"</li> <li>423 "locked"</li> <li>424 "failed dependency"</li> <li>426 "upgrade required"</li> <li>428 "precondition required"</li> <li>429 "too many requests"</li> <li>431 "request header fields too large"</li> <li>500 "internal server error"</li> <li>501 "not implemented"</li> <li>502 "bad gateway"</li> <li>503 "service unavailable"</li> <li>504 "gateway timeout"</li> <li>505 "http version not supported"</li> <li>506 "variant also negotiates"</li> <li>507 "insufficient storage"</li> <li>508 "loop detected"</li> <li>510 "not extended"</li> <li>511 "network authentication required"</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">=></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">'<http://127.0.0.1/>'</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 "charset".</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">// => "image/png"</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 "utf-8". 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">=></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 "back" is special-cased
|
||
to provide Referrer support, when Referrer
|
||
is not present <code>alt</code> or "/" 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 "attachment" 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>"</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 "continue"</li> <li>101 "switching protocols"</li> <li>102 "processing"</li> <li>200 "ok"</li> <li>201 "created"</li> <li>202 "accepted"</li> <li>203 "non-authoritative information"</li> <li>204 "no content"</li> <li>205 "reset content"</li> <li>206 "partial content"</li> <li>207 "multi-status"</li> <li>208 "already reported"</li> <li>226 "im used"</li> <li>300 "multiple choices"</li> <li>301 "moved permanently"</li> <li>302 "found"</li> <li>303 "see other"</li> <li>304 "not modified"</li> <li>305 "use proxy"</li> <li>307 "temporary redirect"</li> <li>308 "permanent redirect"</li> <li>400 "bad request"</li> <li>401 "unauthorized"</li> <li>402 "payment required"</li> <li>403 "forbidden"</li> <li>404 "not found"</li> <li>405 "method not allowed"</li> <li>406 "not acceptable"</li> <li>407 "proxy authentication required"</li> <li>408 "request timeout"</li> <li>409 "conflict"</li> <li>410 "gone"</li> <li>411 "length required"</li> <li>412 "precondition failed"</li> <li>413 "payload too large"</li> <li>414 "uri too long"</li> <li>415 "unsupported media type"</li> <li>416 "range not satisfiable"</li> <li>417 "expectation failed"</li> <li>418 "I'm a teapot"</li> <li>422 "unprocessable entity"</li> <li>423 "locked"</li> <li>424 "failed dependency"</li> <li>426 "upgrade required"</li> <li>428 "precondition required"</li> <li>429 "too many requests"</li> <li>431 "request header fields too large"</li> <li>500 "internal server error"</li> <li>501 "not implemented"</li> <li>502 "bad gateway"</li> <li>503 "service unavailable"</li> <li>504 "gateway timeout"</li> <li>505 "http version not supported"</li> <li>506 "variant also negotiates"</li> <li>507 "insufficient storage"</li> <li>508 "loop detected"</li> <li>510 "not extended"</li> <li>511 "network authentication required"</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">=></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">'<http://127.0.0.1/>'</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 "charset".</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">// => "image/png"</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 "utf-8". 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">=></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 "back" is special-cased
|
||
to provide Referrer support, when Referrer
|
||
is not present <code>alt</code> or "/" 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 "attachment" 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>"</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 Boom’s 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">"statusCode"</span><span class="token operator">:</span> <span class="token number">400</span><span class="token punctuation">,</span>
|
||
<span class="token property">"error"</span><span class="token operator">:</span> <span class="token string">"Bad Request"</span><span class="token punctuation">,</span>
|
||
<span class="token property">"message"</span><span class="token operator">:</span> <span class="token string">"invalid query"</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="value"' 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">"payload"</span><span class="token operator">:</span> <span class="token punctuation">{</span>
|
||
<span class="token property">"statusCode"</span><span class="token operator">:</span> <span class="token number">401</span><span class="token punctuation">,</span>
|
||
<span class="token property">"error"</span><span class="token operator">:</span> <span class="token string">"Unauthorized"</span><span class="token punctuation">,</span>
|
||
<span class="token property">"message"</span><span class="token operator">:</span> <span class="token string">"invalid password"</span>
|
||
<span class="token punctuation">}</span><span class="token punctuation">,</span>
|
||
<span class="token string">"headers"</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">"payload"</span><span class="token operator">:</span> <span class="token punctuation">{</span>
|
||
<span class="token property">"statusCode"</span><span class="token operator">:</span> <span class="token number">401</span><span class="token punctuation">,</span>
|
||
<span class="token property">"error"</span><span class="token operator">:</span> <span class="token string">"Unauthorized"</span><span class="token punctuation">,</span>
|
||
<span class="token property">"message"</span><span class="token operator">:</span> <span class="token string">"invalid password"</span><span class="token punctuation">,</span>
|
||
<span class="token property">"attributes"</span><span class="token operator">:</span> <span class="token punctuation">{</span>
|
||
<span class="token property">"error"</span><span class="token operator">:</span> <span class="token string">"invalid password"</span>
|
||
<span class="token punctuation">}</span>
|
||
<span class="token punctuation">}</span><span class="token punctuation">,</span>
|
||
<span class="token string">"headers"</span> <span class="token punctuation">{</span>
|
||
<span class="token property">"WWW-Authenticate"</span><span class="token operator">:</span> <span class="token string">"sample error=\"invalid password\""</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">"payload"</span><span class="token operator">:</span> <span class="token punctuation">{</span>
|
||
<span class="token property">"statusCode"</span><span class="token operator">:</span> <span class="token number">401</span><span class="token punctuation">,</span>
|
||
<span class="token property">"error"</span><span class="token operator">:</span> <span class="token string">"Unauthorized"</span><span class="token punctuation">,</span>
|
||
<span class="token property">"attributes"</span><span class="token operator">:</span> <span class="token string">"VGhpcyBpcyBhIHRlc3QgdG9rZW4="</span>
|
||
<span class="token punctuation">}</span><span class="token punctuation">,</span>
|
||
<span class="token string">"headers"</span> <span class="token punctuation">{</span>
|
||
<span class="token property">"WWW-Authenticate"</span><span class="token operator">:</span> <span class="token string">"Negotiate VGhpcyBpcyBhIHRlc3QgdG9rZW4="</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">"payload"</span><span class="token operator">:</span> <span class="token punctuation">{</span>
|
||
<span class="token property">"statusCode"</span><span class="token operator">:</span> <span class="token number">401</span><span class="token punctuation">,</span>
|
||
<span class="token property">"error"</span><span class="token operator">:</span> <span class="token string">"Unauthorized"</span><span class="token punctuation">,</span>
|
||
<span class="token property">"message"</span><span class="token operator">:</span> <span class="token string">"invalid password"</span><span class="token punctuation">,</span>
|
||
<span class="token property">"attributes"</span><span class="token operator">:</span> <span class="token punctuation">{</span>
|
||
<span class="token property">"error"</span><span class="token operator">:</span> <span class="token string">"invalid password"</span><span class="token punctuation">,</span>
|
||
<span class="token property">"ttl"</span><span class="token operator">:</span> <span class="token number">0</span><span class="token punctuation">,</span>
|
||
<span class="token property">"cache"</span><span class="token operator">:</span> <span class="token string">""</span><span class="token punctuation">,</span>
|
||
<span class="token property">"foo"</span><span class="token operator">:</span> <span class="token string">"bar"</span>
|
||
<span class="token punctuation">}</span>
|
||
<span class="token punctuation">}</span><span class="token punctuation">,</span>
|
||
<span class="token string">"headers"</span> <span class="token punctuation">{</span>
|
||
<span class="token property">"WWW-Authenticate"</span><span class="token operator">:</span> <span class="token string">"sample ttl=\"0\", cache=\"\", foo=\"bar\", error=\"invalid password\""</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">"statusCode"</span><span class="token operator">:</span> <span class="token number">402</span><span class="token punctuation">,</span>
|
||
<span class="token property">"error"</span><span class="token operator">:</span> <span class="token string">"Payment Required"</span><span class="token punctuation">,</span>
|
||
<span class="token property">"message"</span><span class="token operator">:</span> <span class="token string">"bandwidth used"</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">"statusCode"</span><span class="token operator">:</span> <span class="token number">403</span><span class="token punctuation">,</span>
|
||
<span class="token property">"error"</span><span class="token operator">:</span> <span class="token string">"Forbidden"</span><span class="token punctuation">,</span>
|
||
<span class="token property">"message"</span><span class="token operator">:</span> <span class="token string">"try again some time"</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">"statusCode"</span><span class="token operator">:</span> <span class="token number">404</span><span class="token punctuation">,</span>
|
||
<span class="token property">"error"</span><span class="token operator">:</span> <span class="token string">"Not Found"</span><span class="token punctuation">,</span>
|
||
<span class="token property">"message"</span><span class="token operator">:</span> <span class="token string">"missing"</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">"statusCode"</span><span class="token operator">:</span> <span class="token number">405</span><span class="token punctuation">,</span>
|
||
<span class="token property">"error"</span><span class="token operator">:</span> <span class="token string">"Method Not Allowed"</span><span class="token punctuation">,</span>
|
||
<span class="token property">"message"</span><span class="token operator">:</span> <span class="token string">"that method is not allowed"</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">"statusCode"</span><span class="token operator">:</span> <span class="token number">406</span><span class="token punctuation">,</span>
|
||
<span class="token property">"error"</span><span class="token operator">:</span> <span class="token string">"Not Acceptable"</span><span class="token punctuation">,</span>
|
||
<span class="token property">"message"</span><span class="token operator">:</span> <span class="token string">"unacceptable"</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">"statusCode"</span><span class="token operator">:</span> <span class="token number">407</span><span class="token punctuation">,</span>
|
||
<span class="token property">"error"</span><span class="token operator">:</span> <span class="token string">"Proxy Authentication Required"</span><span class="token punctuation">,</span>
|
||
<span class="token property">"message"</span><span class="token operator">:</span> <span class="token string">"auth missing"</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">"statusCode"</span><span class="token operator">:</span> <span class="token number">408</span><span class="token punctuation">,</span>
|
||
<span class="token property">"error"</span><span class="token operator">:</span> <span class="token string">"Request Time-out"</span><span class="token punctuation">,</span>
|
||
<span class="token property">"message"</span><span class="token operator">:</span> <span class="token string">"timed out"</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">"statusCode"</span><span class="token operator">:</span> <span class="token number">409</span><span class="token punctuation">,</span>
|
||
<span class="token property">"error"</span><span class="token operator">:</span> <span class="token string">"Conflict"</span><span class="token punctuation">,</span>
|
||
<span class="token property">"message"</span><span class="token operator">:</span> <span class="token string">"there was a conflict"</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">"statusCode"</span><span class="token operator">:</span> <span class="token number">410</span><span class="token punctuation">,</span>
|
||
<span class="token property">"error"</span><span class="token operator">:</span> <span class="token string">"Gone"</span><span class="token punctuation">,</span>
|
||
<span class="token property">"message"</span><span class="token operator">:</span> <span class="token string">"it is gone"</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">"statusCode"</span><span class="token operator">:</span> <span class="token number">411</span><span class="token punctuation">,</span>
|
||
<span class="token property">"error"</span><span class="token operator">:</span> <span class="token string">"Length Required"</span><span class="token punctuation">,</span>
|
||
<span class="token property">"message"</span><span class="token operator">:</span> <span class="token string">"length needed"</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">"statusCode"</span><span class="token operator">:</span> <span class="token number">412</span><span class="token punctuation">,</span>
|
||
<span class="token property">"error"</span><span class="token operator">:</span> <span class="token string">"Precondition Failed"</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">"statusCode"</span><span class="token operator">:</span> <span class="token number">413</span><span class="token punctuation">,</span>
|
||
<span class="token property">"error"</span><span class="token operator">:</span> <span class="token string">"Request Entity Too Large"</span><span class="token punctuation">,</span>
|
||
<span class="token property">"message"</span><span class="token operator">:</span> <span class="token string">"too big"</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">"statusCode"</span><span class="token operator">:</span> <span class="token number">414</span><span class="token punctuation">,</span>
|
||
<span class="token property">"error"</span><span class="token operator">:</span> <span class="token string">"Request-URI Too Large"</span><span class="token punctuation">,</span>
|
||
<span class="token property">"message"</span><span class="token operator">:</span> <span class="token string">"uri is too long"</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">"statusCode"</span><span class="token operator">:</span> <span class="token number">415</span><span class="token punctuation">,</span>
|
||
<span class="token property">"error"</span><span class="token operator">:</span> <span class="token string">"Unsupported Media Type"</span><span class="token punctuation">,</span>
|
||
<span class="token property">"message"</span><span class="token operator">:</span> <span class="token string">"that media is not supported"</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">"statusCode"</span><span class="token operator">:</span> <span class="token number">416</span><span class="token punctuation">,</span>
|
||
<span class="token property">"error"</span><span class="token operator">:</span> <span class="token string">"Requested Range Not Satisfiable"</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">"statusCode"</span><span class="token operator">:</span> <span class="token number">417</span><span class="token punctuation">,</span>
|
||
<span class="token property">"error"</span><span class="token operator">:</span> <span class="token string">"Expectation Failed"</span><span class="token punctuation">,</span>
|
||
<span class="token property">"message"</span><span class="token operator">:</span> <span class="token string">"expected this to work"</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">"statusCode"</span><span class="token operator">:</span> <span class="token number">418</span><span class="token punctuation">,</span>
|
||
<span class="token property">"error"</span><span class="token operator">:</span> <span class="token string">"I'm a Teapot"</span><span class="token punctuation">,</span>
|
||
<span class="token property">"message"</span><span class="token operator">:</span> <span class="token string">"Sorry, no coffee..."</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">"statusCode"</span><span class="token operator">:</span> <span class="token number">422</span><span class="token punctuation">,</span>
|
||
<span class="token property">"error"</span><span class="token operator">:</span> <span class="token string">"Unprocessable Entity"</span><span class="token punctuation">,</span>
|
||
<span class="token property">"message"</span><span class="token operator">:</span> <span class="token string">"your data is bad and you should feel bad"</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">"statusCode"</span><span class="token operator">:</span> <span class="token number">423</span><span class="token punctuation">,</span>
|
||
<span class="token property">"error"</span><span class="token operator">:</span> <span class="token string">"Locked"</span><span class="token punctuation">,</span>
|
||
<span class="token property">"message"</span><span class="token operator">:</span> <span class="token string">"this resource has been locked"</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">"statusCode"</span><span class="token operator">:</span> <span class="token number">428</span><span class="token punctuation">,</span>
|
||
<span class="token property">"error"</span><span class="token operator">:</span> <span class="token string">"Precondition Required"</span><span class="token punctuation">,</span>
|
||
<span class="token property">"message"</span><span class="token operator">:</span> <span class="token string">"you must supply an If-Match header"</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">"statusCode"</span><span class="token operator">:</span> <span class="token number">429</span><span class="token punctuation">,</span>
|
||
<span class="token property">"error"</span><span class="token operator">:</span> <span class="token string">"Too Many Requests"</span><span class="token punctuation">,</span>
|
||
<span class="token property">"message"</span><span class="token operator">:</span> <span class="token string">"you have exceeded your request limit"</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">"statusCode"</span><span class="token operator">:</span> <span class="token number">451</span><span class="token punctuation">,</span>
|
||
<span class="token property">"error"</span><span class="token operator">:</span> <span class="token string">"Unavailable For Legal Reasons"</span><span class="token punctuation">,</span>
|
||
<span class="token property">"message"</span><span class="token operator">:</span> <span class="token string">"you are not permitted to view this resource for legal reasons"</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">"statusCode"</span><span class="token operator">:</span> <span class="token number">500</span><span class="token punctuation">,</span>
|
||
<span class="token property">"error"</span><span class="token operator">:</span> <span class="token string">"Internal Server Error"</span><span class="token punctuation">,</span>
|
||
<span class="token property">"message"</span><span class="token operator">:</span> <span class="token string">"An internal server error occurred"</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">"statusCode"</span><span class="token operator">:</span> <span class="token number">501</span><span class="token punctuation">,</span>
|
||
<span class="token property">"error"</span><span class="token operator">:</span> <span class="token string">"Not Implemented"</span><span class="token punctuation">,</span>
|
||
<span class="token property">"message"</span><span class="token operator">:</span> <span class="token string">"method not implemented"</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">"statusCode"</span><span class="token operator">:</span> <span class="token number">502</span><span class="token punctuation">,</span>
|
||
<span class="token property">"error"</span><span class="token operator">:</span> <span class="token string">"Bad Gateway"</span><span class="token punctuation">,</span>
|
||
<span class="token property">"message"</span><span class="token operator">:</span> <span class="token string">"that is a bad gateway"</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">"statusCode"</span><span class="token operator">:</span> <span class="token number">503</span><span class="token punctuation">,</span>
|
||
<span class="token property">"error"</span><span class="token operator">:</span> <span class="token string">"Service Unavailable"</span><span class="token punctuation">,</span>
|
||
<span class="token property">"message"</span><span class="token operator">:</span> <span class="token string">"unavailable"</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">"statusCode"</span><span class="token operator">:</span> <span class="token number">504</span><span class="token punctuation">,</span>
|
||
<span class="token property">"error"</span><span class="token operator">:</span> <span class="token string">"Gateway Time-out"</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>
|