mirror of
https://github.com/strapi/strapi.git
synced 2025-08-19 06:08:50 +00:00
175 lines
43 KiB
HTML
175 lines
43 KiB
HTML
![]() |
<!DOCTYPE html>
|
||
|
<html lang="en-US">
|
||
|
<head>
|
||
|
<meta charset="utf-8">
|
||
|
<meta name="viewport" content="width=device-width,initial-scale=1">
|
||
|
<title>Request | 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/18.e1f1758f.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/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/51.b67aee1b.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="/docum
|
||
|
<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/">Version 3.x.x</option><option value="/1.x.x/" selected="selected">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/1.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><a href="/documentation/1.x.x/admin.html" class="sidebar-link">Admin</a></li><li><a href="/documentation/1.x.x/configuration.html" class="sidebar-link">Configuration</a></li><li><a href="/documentation/1.x.x/email.html" class="sidebar-link">Email</a></li><li><a href="/documentation/1.x.x/introduction.html" class="sidebar-link">Introduction</a></li><li><a href="/documentation/1.x.x/queries.html" class="sidebar-link">Query Interface</a></li><li><a href="/documentation/1.x.x/response.html" class="sidebar-link">Response</a></li><li><a href="/documentation/1.x.x/sessions.html" class="sidebar-link">Sessions</a></li><li><a href="/documentation/1.x.x/testing.html" class="sidebar-link">Testing</a></li><li><a href="/documentation/1.x.x/views.html" class="sidebar-link">Views</a></li><li><a href="/documentation/1.x.x/blueprints.html" class="sidebar-link">Blueprints</a></li><li><a href="/documentation/1.x.x/context.html" class="sidebar-link">Context</a></li><li><a href="/documentation/1.x.x/graphql.html" class="sidebar-link">GraphQL</a></li><li><a href="/documentation/1.x.x/logging.html" class="sidebar-link">Logging</a></li><li><a href="/documentation/1.x.x/router.html" class="sidebar-link">Router</a></li><li><a href="/documentation/1.x.x/upload.html" class="sidebar-link">Upload</a></li><li><a href="/documentation/1.x.x/cli.html" class="sidebar-link">CLI</a></li><li><a href="/documentation/1.x.x/customization.html" class="sidebar-link">Customization</a></li><li><a href="/documentation/1.x.x/internationalization.html" class="sidebar-link">Internationalization</a></li><li><a href="/documentation/1.x.x/models.html" class="sidebar-link">Models</a></li><li><a href="/documentation/1.x.x/request.html" class="active sidebar-link">Request</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header"><a href="/documentation/1.x.x/request.html#api" class="sidebar-link">API</a></li></ul></li><li><a href="/documentation/1.x.x/services.html" class="sidebar-link">Services</a></li><li><a href="/documentation/1.x.x/users.html" class="sidebar-link">Users</a></li></ul> </div> <div class="page"><div class="content"><h1 id="request"><a href="#request" aria-hidden="true" class="header-anchor">#</a> Request</h1> <p>A Strapi <code>Request</code> object is an abstraction on top of Node's vanilla request object,
|
||
|
providing additional functionality that is useful for every day HTTP server
|
||
|
development.</p> <h2 id="api"><a href="#api" aria-hidden="true" class="header-anchor">#</a> API</h2> <h3 id="request-header"><a href="#request-header" aria-hidden="true" class="header-anchor">#</a> request.header</h3> <p>Request header object.</p> <h3 id="request-headers"><a href="#request-headers" aria-hidden="true" class="header-anchor">#</a> request.headers</h3> <p>Request header object. Alias as <code>request.header</code>.</p> <h3 id="request-method"><a href="#request-method" aria-hidden="true" class="header-anchor">#</a> request.method</h3> <p>Request method.</p> <h3 id="request-method-2"><a href="#request-method-2" aria-hidden="true" class="header-anchor">#</a> request.method=</h3> <p>Set request method, useful for implementing middleware
|
||
|
such as <code>methodOverride()</code>.</p> <h3 id="request-length"><a href="#request-length" aria-hidden="true" class="header-anchor">#</a> request.length</h3> <p>Return request Content-Length as a number when present, or <code>undefined</code>.</p> <h3 id="request-url"><a href="#request-url" aria-hidden="true" class="header-anchor">#</a> request.url</h3> <p>Get request URL.</p> <h3 id="request-url-2"><a href="#request-url-2" aria-hidden="true" class="header-anchor">#</a> request.url=</h3> <p>Set request URL, useful for url rewrites.</p> <h3 id="request-originalurl"><a href="#request-originalurl" aria-hidden="true" class="header-anchor">#</a> request.originalUrl</h3> <p>Get request original URL.</p> <h3 id="request-origin"><a href="#request-origin" aria-hidden="true" class="header-anchor">#</a> request.origin</h3> <p>Get origin of URL, include <code>protocol</code> and <code>host</code>.</p> <div class="language-js extra-class"><pre class="language-js"><code><span class="token keyword">this</span><span class="token punctuation">.</span>request<span class="token punctuation">.</span>origin
|
||
|
<span class="token comment">// => http://example.com</span>
|
||
|
</code></pre></div><h3 id="request-href"><a href="#request-href" aria-hidden="true" class="header-anchor">#</a> request.href</h3> <p>Get full request URL, include <code>protocol</code>, <code>host</code> and <code>url</code>.</p> <div class="language-js extra-class"><pre class="language-js"><code><span class="token keyword">this</span><span class="token punctuation">.</span>request<span class="token punctuation">.</span>href
|
||
|
<span class="token comment">// => http://example.com/foo/bar?q=1</span>
|
||
|
</code></pre></div><h3 id="request-path"><a href="#request-path" aria-hidden="true" class="header-anchor">#</a> request.path</h3> <p>Get request pathname.</p> <h3 id="request-path-2"><a href="#request-path-2" aria-hidden="true" class="header-anchor">#</a> request.path=</h3> <p>Set request pathname and retain query-string when present.</p> <h3 id="request-querystring"><a href="#request-querystring" aria-hidden="true" class="header-anchor">#</a> request.querystring</h3> <p>Get raw query string void of <code>?</code>.</p> <h3 id="request-querystring-2"><a href="#request-querystring-2" aria-hidden="true" class="header-anchor">#</a> request.querystring=</h3> <p>Set raw query string.</p> <h3 id="request-search"><a href="#request-search" aria-hidden="true" class="header-anchor">#</a> request.search</h3> <p>Get raw query string with the <code>?</code>.</p> <h3 id="request-search-2"><a href="#request-search-2" aria-hidden="true" class="header-anchor">#</a> request.search=</h3> <p>Set raw query string.</p> <h3 id="request-host"><a href="#request-host" aria-hidden="true" class="header-anchor">#</a> request.host</h3> <p>Get host (hostname:port) when present. Supports <code>X-Forwarded-Host</code>
|
||
|
when <code>strapi.app.proxy</code> is <code>true</code>, otherwise <code>Host</code> is used.</p> <h3 id="request-hostname"><a href="#request-hostname" aria-hidden="true" class="header-anchor">#</a> request.hostname</h3> <p>Get hostname when present. Supports <code>X-Forwarded-Host</code>
|
||
|
when <code>strapi.app.proxy</code> is <code>true</code>, otherwise <code>Host</code> is used.</p> <h3 id="request-type"><a href="#request-type" aria-hidden="true" class="header-anchor">#</a> request.type</h3> <p>Get request <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> <span class="token keyword">this</span><span class="token punctuation">.</span>request<span class="token punctuation">.</span>type<span class="token punctuation">;</span>
|
||
|
<span class="token comment">// => "image/png"</span>
|
||
|
</code></pre></div><h3 id="request-charset"><a href="#request-charset" aria-hidden="true" class="header-anchor">#</a> request.charset</h3> <p>Get request charset when present, or <code>undefined</code>:</p> <div class="language-js extra-class"><pre class="language-js"><code><span class="token keyword">this</span><span class="token punctuation">.</span>request<span class="token punctuation">.</span>charset
|
||
|
<span class="token comment">// => "utf-8"</span>
|
||
|
</code></pre></div><h3 id="request-query"><a href="#request-query" aria-hidden="true" class="header-anchor">#</a> request.query</h3> <p>Get parsed query-string, returning an empty object when no
|
||
|
query-string is present. Note that this getter does <em>not</em>
|
||
|
support nested parsing.</p> <p>For example "color=blue&size=small":</p> <div class="language-js extra-class"><pre class="language-js"><code><span class="token punctuation">{</span>
|
||
|
color<span class="token punctuation">:</span> <span class="token string">'blue'</span><span class="token punctuation">,</span>
|
||
|
size<span class="token punctuation">:</span> <span class="token string">'small'</span>
|
||
|
<span class="token punctuation">}</span>
|
||
|
</code></pre></div><h3 id="request-query-2"><a href="#request-query-2" aria-hidden="true" class="header-anchor">#</a> request.query=</h3> <p>Set query-string to the given object. Note that this
|
||
|
setter does <em>not</em> support nested objects.</p> <div class="language-js extra-class"><pre class="language-js"><code><span class="token keyword">this</span><span class="token punctuation">.</span>query <span class="token operator">=</span> <span class="token punctuation">{</span> next<span class="token punctuation">:</span> <span class="token string">'/login'</span> <span class="token punctuation">}</span><span class="token punctuation">;</span>
|
||
|
</code></pre></div><h3 id="request-fresh"><a href="#request-fresh" aria-hidden="true" class="header-anchor">#</a> request.fresh</h3> <p>Check if a request cache is "fresh", aka the contents have not changed. This
|
||
|
method is for cache negotiation between <code>If-None-Match</code> / <code>ETag</code>, and
|
||
|
<code>If-Modified-Since</code> and <code>Last-Modified</code>. It should be referenced after setting
|
||
|
one or more of these response headers.</p> <div class="language-js extra-class"><pre class="language-js"><code><span class="token comment">// freshness check requires status 20x or 304</span>
|
||
|
<span class="token keyword">this</span><span class="token punctuation">.</span>status <span class="token operator">=</span> <span class="token number">200</span><span class="token punctuation">;</span>
|
||
|
<span class="token keyword">this</span><span class="token punctuation">.</span><span class="token keyword">set</span><span class="token punctuation">(</span><span class="token string">'ETag'</span><span class="token punctuation">,</span> <span class="token string">'123'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
|
||
|
|
||
|
<span class="token comment">// cache is ok</span>
|
||
|
<span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token keyword">this</span><span class="token punctuation">.</span>fresh<span class="token punctuation">)</span> <span class="token punctuation">{</span>
|
||
|
<span class="token keyword">this</span><span class="token punctuation">.</span>status <span class="token operator">=</span> <span class="token number">304</span><span class="token punctuation">;</span>
|
||
|
<span class="token keyword">return</span><span class="token punctuation">;</span>
|
||
|
<span class="token punctuation">}</span>
|
||
|
|
||
|
<span class="token comment">// cache is stale</span>
|
||
|
<span class="token comment">// fetch new data</span>
|
||
|
<span class="token keyword">this</span><span class="token punctuation">.</span>body <span class="token operator">=</span> <span class="token keyword">yield</span> db<span class="token punctuation">.</span><span class="token function">find</span><span class="token punctuation">(</span><span class="token string">'something'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
|
||
|
</code></pre></div><h3 id="request-stale"><a href="#request-stale" aria-hidden="true" class="header-anchor">#</a> request.stale</h3> <p>Inverse of <code>request.fresh</code>.</p> <h3 id="request-protocol"><a href="#request-protocol" aria-hidden="true" class="header-anchor">#</a> request.protocol</h3> <p>Return request protocol, "https" or "http". Supports <code>X-Forwarded-Proto</code>
|
||
|
when <code>strapi.app.proxy</code> is <code>true</code>.</p> <h3 id="request-secure"><a href="#request-secure" aria-hidden="true" class="header-anchor">#</a> request.secure</h3> <p>Shorthand for <code>this.protocol == "https"</code> to check if a request was
|
||
|
issued via TLS.</p> <h3 id="request-ip"><a href="#request-ip" aria-hidden="true" class="header-anchor">#</a> request.ip</h3> <p>Request remote address. Supports <code>X-Forwarded-For</code> when <code>strapi.app.proxy</code>
|
||
|
is <code>true</code>.</p> <h3 id="request-ips"><a href="#request-ips" aria-hidden="true" class="header-anchor">#</a> request.ips</h3> <p>When <code>X-Forwarded-For</code> is present and <code>strapi.app.proxy</code> is enabled an array
|
||
|
of these ips is returned, ordered from upstream -> downstream. When disabled
|
||
|
an empty array is returned.</p> <h3 id="request-subdomains"><a href="#request-subdomains" aria-hidden="true" class="header-anchor">#</a> request.subdomains</h3> <p>Return subdomains as an array.</p> <p>Subdomains are the dot-separated parts of the host before the main domain of
|
||
|
the app. By default, the domain of the app is assumed to be the last two
|
||
|
parts of the host. This can be changed by setting <code>strapi.app.subdomainOffset</code>.</p> <p>For example, if the domain is "tobi.ferrets.example.com":
|
||
|
If <code>strapi.app.subdomainOffset</code> is not set, this.subdomains is <code>["ferrets", "tobi"]</code>.
|
||
|
If <code>strapi.app.subdomainOffset</code> is 3, this.subdomains is <code>["tobi"]</code>.</p> <h3 id="request-is-types"><a href="#request-is-types" aria-hidden="true" class="header-anchor">#</a> request.is(types...)</h3> <p>Check if the incoming request contains the "Content-Type"
|
||
|
header field, and it contains any of the give mime <code>type</code>s.
|
||
|
If there is no request body, <code>undefined</code> is returned.
|
||
|
If there is no content type, or the match fails <code>false</code> is returned.
|
||
|
Otherwise, it returns the matching content-type.</p> <div class="language-js extra-class"><pre class="language-js"><code><span class="token comment">// With Content-Type: text/html; charset=utf-8</span>
|
||
|
<span class="token keyword">this</span><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 comment">// => 'html'</span>
|
||
|
<span class="token keyword">this</span><span class="token punctuation">.</span><span class="token function">is</span><span class="token punctuation">(</span><span class="token string">'text/html'</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// => 'text/html'</span>
|
||
|
<span class="token keyword">this</span><span class="token punctuation">.</span><span class="token function">is</span><span class="token punctuation">(</span><span class="token string">'text/*'</span><span class="token punctuation">,</span> <span class="token string">'text/html'</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// => 'text/html'</span>
|
||
|
|
||
|
<span class="token operator">/</span><span class="token operator">/</span> When Content<span class="token operator">-</span>Type is application<span class="token operator">/</span>json
|
||
|
<span class="token keyword">this</span><span class="token punctuation">.</span><span class="token function">is</span><span class="token punctuation">(</span><span class="token string">'json'</span><span class="token punctuation">,</span> <span class="token string">'urlencoded'</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token operator">/</span><span class="token operator">/</span> <span class="token operator">=></span> <span class="token string">'json'</span>
|
||
|
<span class="token keyword">this</span><span class="token punctuation">.</span><span class="token function">is</span><span class="token punctuation">(</span><span class="token string">'application/json'</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token operator">/</span><span class="token operator">/</span> <span class="token operator">=></span> <span class="token string">'application/json'</span>
|
||
|
<span class="token keyword">this</span><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 string">'application/*'</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// => 'application/json'</span>
|
||
|
|
||
|
<span class="token keyword">this</span><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 operator">/</span><span class="token operator">/</span> <span class="token operator">=></span> <span class="token boolean">false</span>
|
||
|
</code></pre></div><p>For example if you want to ensure that
|
||
|
only images are sent to a given route:</p> <div class="language-js extra-class"><pre class="language-js"><code><span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token keyword">this</span><span class="token punctuation">.</span><span class="token function">is</span><span class="token punctuation">(</span><span class="token string">'image/*'</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
|
||
|
<span class="token comment">// process</span>
|
||
|
<span class="token punctuation">}</span> <span class="token keyword">else</span> <span class="token punctuation">{</span>
|
||
|
<span class="token keyword">this</span><span class="token punctuation">.</span><span class="token keyword">throw</span><span class="token punctuation">(</span><span class="token number">415</span><span class="token punctuation">,</span> <span class="token string">'images only!'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
|
||
|
<span class="token punctuation">}</span>
|
||
|
</code></pre></div><h3 id="content-negotiation"><a href="#content-negotiation" aria-hidden="true" class="header-anchor">#</a> Content Negotiation</h3> <p>Strapi's <code>request</code> object includes helpful content negotiation utilities powered by
|
||
|
<a href="http://github.com/expressjs/accepts" target="_blank" rel="noopener noreferrer">accepts<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> and
|
||
|
<a href="https://github.com/federomero/negotiator" target="_blank" rel="noopener noreferrer">negotiator<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> <p>These utilities are:</p> <ul><li><code>request.accepts(types)</code></li> <li><code>request.acceptsEncodings(types)</code></li> <li><code>request.acceptsCharsets(charsets)</code></li> <li><code>request.acceptsLanguages(langs)</code></li></ul> <p>If no types are supplied, all acceptable types are returned.</p> <p>If multiple types are supplied, the best match will be returned. If no matches are found,
|
||
|
a <code>false</code> is returned, and you should send a <code>406 "Not Acceptable"</code> response to the client.</p> <p>In the case of missing accept headers where any type is acceptable, the first type will
|
||
|
be returned. Thus, the order of types you supply is important.</p> <h3 id="request-accepts-types"><a href="#request-accepts-types" aria-hidden="true" class="header-anchor">#</a> request.accepts(types)</h3> <p>Check if the given <code>type(s)</code> is acceptable, returning the best match when true, otherwise
|
||
|
<code>false</code>. The <code>type</code> value may be one or more mime type string
|
||
|
such as "application/json", the extension name
|
||
|
such as "json", or an array <code>["json", "html", "text/plain"]</code>.</p> <div class="language-js extra-class"><pre class="language-js"><code><span class="token comment">// Accept: text/html</span>
|
||
|
<span class="token keyword">this</span><span class="token punctuation">.</span><span class="token function">accepts</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 comment">// => "html"</span>
|
||
|
|
||
|
<span class="token comment">// Accept: text/*, application/json</span>
|
||
|
<span class="token keyword">this</span><span class="token punctuation">.</span><span class="token function">accepts</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 comment">// => "html"</span>
|
||
|
<span class="token keyword">this</span><span class="token punctuation">.</span><span class="token function">accepts</span><span class="token punctuation">(</span><span class="token string">'text/html'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
|
||
|
<span class="token comment">// => "text/html"</span>
|
||
|
<span class="token keyword">this</span><span class="token punctuation">.</span><span class="token function">accepts</span><span class="token punctuation">(</span><span class="token string">'json'</span><span class="token punctuation">,</span> <span class="token string">'text'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
|
||
|
<span class="token comment">// => "json"</span>
|
||
|
<span class="token keyword">this</span><span class="token punctuation">.</span><span class="token function">accepts</span><span class="token punctuation">(</span><span class="token string">'application/json'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
|
||
|
<span class="token comment">// => "application/json"</span>
|
||
|
|
||
|
<span class="token comment">// Accept: text/*, application/json</span>
|
||
|
<span class="token keyword">this</span><span class="token punctuation">.</span><span class="token function">accepts</span><span class="token punctuation">(</span><span class="token string">'image/png'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
|
||
|
<span class="token keyword">this</span><span class="token punctuation">.</span><span class="token function">accepts</span><span class="token punctuation">(</span><span class="token string">'png'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
|
||
|
<span class="token comment">// => false</span>
|
||
|
|
||
|
<span class="token comment">// Accept: text/*;q=.5, application/json</span>
|
||
|
<span class="token keyword">this</span><span class="token punctuation">.</span><span class="token function">accepts</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token string">'html'</span><span class="token punctuation">,</span> <span class="token string">'json'</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
|
||
|
<span class="token keyword">this</span><span class="token punctuation">.</span><span class="token function">accepts</span><span class="token punctuation">(</span><span class="token string">'html'</span><span class="token punctuation">,</span> <span class="token string">'json'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
|
||
|
<span class="token comment">// => "json"</span>
|
||
|
|
||
|
<span class="token comment">// No Accept header</span>
|
||
|
<span class="token keyword">this</span><span class="token punctuation">.</span><span class="token function">accepts</span><span class="token punctuation">(</span><span class="token string">'html'</span><span class="token punctuation">,</span> <span class="token string">'json'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
|
||
|
<span class="token comment">// => "html"</span>
|
||
|
<span class="token keyword">this</span><span class="token punctuation">.</span><span class="token function">accepts</span><span class="token punctuation">(</span><span class="token string">'json'</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 comment">// => "json"</span>
|
||
|
</code></pre></div><p>You may call <code>this.accepts()</code> as many times as you like,
|
||
|
or use a switch:</p> <div class="language-js extra-class"><pre class="language-js"><code><span class="token keyword">switch</span> <span class="token punctuation">(</span><span class="token keyword">this</span><span class="token punctuation">.</span><span class="token function">accepts</span><span class="token punctuation">(</span><span class="token string">'json'</span><span class="token punctuation">,</span> <span class="token string">'html'</span><span class="token punctuation">,</span> <span class="token string">'text'</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
|
||
|
<span class="token keyword">case</span> <span class="token string">'json'</span><span class="token punctuation">:</span> <span class="token keyword">break</span><span class="token punctuation">;</span>
|
||
|
<span class="token keyword">case</span> <span class="token string">'html'</span><span class="token punctuation">:</span> <span class="token keyword">break</span><span class="token punctuation">;</span>
|
||
|
<span class="token keyword">case</span> <span class="token string">'text'</span><span class="token punctuation">:</span> <span class="token keyword">break</span><span class="token punctuation">;</span>
|
||
|
<span class="token keyword">default</span><span class="token punctuation">:</span> <span class="token keyword">this</span><span class="token punctuation">.</span><span class="token keyword">throw</span><span class="token punctuation">(</span><span class="token number">406</span><span class="token punctuation">,</span> <span class="token string">'json, html, or text only'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
|
||
|
<span class="token punctuation">}</span>
|
||
|
</code></pre></div><h3 id="request-acceptsencodings-encodings"><a href="#request-acceptsencodings-encodings" aria-hidden="true" class="header-anchor">#</a> request.acceptsEncodings(encodings)</h3> <p>Check if <code>encodings</code> are acceptable, returning the best match when true, otherwise <code>false</code>.
|
||
|
Note that you should include <code>identity</code> as one of the encodings!</p> <div class="language-js extra-class"><pre class="language-js"><code><span class="token comment">// Accept-Encoding: gzip</span>
|
||
|
<span class="token keyword">this</span><span class="token punctuation">.</span><span class="token function">acceptsEncodings</span><span class="token punctuation">(</span><span class="token string">'gzip'</span><span class="token punctuation">,</span> <span class="token string">'deflate'</span><span class="token punctuation">,</span> <span class="token string">'identity'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
|
||
|
<span class="token comment">// => "gzip"</span>
|
||
|
|
||
|
<span class="token keyword">this</span><span class="token punctuation">.</span><span class="token function">acceptsEncodings</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token string">'gzip'</span><span class="token punctuation">,</span> <span class="token string">'deflate'</span><span class="token punctuation">,</span> <span class="token string">'identity'</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
|
||
|
<span class="token comment">// => "gzip"</span>
|
||
|
</code></pre></div><p>When no arguments are given all accepted encodings
|
||
|
are returned as an array:</p> <div class="language-js extra-class"><pre class="language-js"><code><span class="token comment">// Accept-Encoding: gzip, deflate</span>
|
||
|
<span class="token keyword">this</span><span class="token punctuation">.</span><span class="token function">acceptsEncodings</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
|
||
|
<span class="token comment">// => ["gzip", "deflate", "identity"]</span>
|
||
|
</code></pre></div><p>Note that the <code>identity</code> encoding (which means no encoding) could be unacceptable if
|
||
|
the client explicitly sends <code>identity;q=0</code>. Although this is an edge case, you should
|
||
|
still handle the case where this method returns <code>false</code>.</p> <h3 id="request-acceptscharsets-charsets"><a href="#request-acceptscharsets-charsets" aria-hidden="true" class="header-anchor">#</a> request.acceptsCharsets(charsets)</h3> <p>Check if <code>charsets</code> are acceptable, returning
|
||
|
the best match when true, otherwise <code>false</code>.</p> <div class="language-js extra-class"><pre class="language-js"><code><span class="token comment">// Accept-Charset: utf-8, iso-8859-1;q=0.2, utf-7;q=0.5</span>
|
||
|
<span class="token keyword">this</span><span class="token punctuation">.</span><span class="token function">acceptsCharsets</span><span class="token punctuation">(</span><span class="token string">'utf-8'</span><span class="token punctuation">,</span> <span class="token string">'utf-7'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
|
||
|
<span class="token comment">// => "utf-8"</span>
|
||
|
|
||
|
<span class="token keyword">this</span><span class="token punctuation">.</span><span class="token function">acceptsCharsets</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token string">'utf-7'</span><span class="token punctuation">,</span> <span class="token string">'utf-8'</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
|
||
|
<span class="token comment">// => "utf-8"</span>
|
||
|
</code></pre></div><p>When no arguments are given all accepted charsets
|
||
|
are returned as an array:</p> <div class="language-js extra-class"><pre class="language-js"><code><span class="token comment">// Accept-Charset: utf-8, iso-8859-1;q=0.2, utf-7;q=0.5</span>
|
||
|
<span class="token keyword">this</span><span class="token punctuation">.</span><span class="token function">acceptsCharsets</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
|
||
|
<span class="token comment">// => ["utf-8", "utf-7", "iso-8859-1"]</span>
|
||
|
</code></pre></div><h3 id="request-acceptslanguages-langs"><a href="#request-acceptslanguages-langs" aria-hidden="true" class="header-anchor">#</a> request.acceptsLanguages(langs)</h3> <p>Check if <code>langs</code> are acceptable, returning
|
||
|
the best match when true, otherwise <code>false</code>.</p> <div class="language-js extra-class"><pre class="language-js"><code><span class="token comment">// Accept-Language: en;q=0.8, es, pt</span>
|
||
|
<span class="token keyword">this</span><span class="token punctuation">.</span><span class="token function">acceptsLanguages</span><span class="token punctuation">(</span><span class="token string">'es'</span><span class="token punctuation">,</span> <span class="token string">'en'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
|
||
|
<span class="token comment">// => "es"</span>
|
||
|
|
||
|
<span class="token keyword">this</span><span class="token punctuation">.</span><span class="token function">acceptsLanguages</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token string">'en'</span><span class="token punctuation">,</span> <span class="token string">'es'</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
|
||
|
<span class="token comment">// => "es"</span>
|
||
|
</code></pre></div><p>When no arguments are given all accepted languages
|
||
|
are returned as an array:</p> <div class="language-js extra-class"><pre class="language-js"><code><span class="token comment">// Accept-Language: en;q=0.8, es, pt</span>
|
||
|
<span class="token keyword">this</span><span class="token punctuation">.</span><span class="token function">acceptsLanguages</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
|
||
|
<span class="token comment">// => ["es", "pt", "en"]</span>
|
||
|
</code></pre></div><h3 id="request-idempotent"><a href="#request-idempotent" aria-hidden="true" class="header-anchor">#</a> request.idempotent</h3> <p>Check if the request is idempotent.</p> <h3 id="request-socket"><a href="#request-socket" aria-hidden="true" class="header-anchor">#</a> request.socket</h3> <p>Return the request socket.</p> <h3 id="request-get-field"><a href="#request-get-field" aria-hidden="true" class="header-anchor">#</a> request.get(field)</h3> <p>Return request header.</p></div> <div class="page-edit"><div class="edit-link"><a href="https://github.com/strapi/strapi/edit/master/docs/1.x.x/request.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 class="page-nav"><p class="inner"><span class="prev">
|
||
|
← <a href="/documentation/1.x.x/models.html" class="prev">
|
||
|
Models
|
||
|
</a></span> <span class="next"><a href="/documentation/1.x.x/services.html">
|
||
|
Services
|
||
|
</a> →
|
||
|
</span></p></div> </div></div></div>
|
||
|
<script src="/documentation/assets/js/18.e1f1758f.js" defer></script><script src="/documentation/assets/js/app.a8210063.js" defer></script>
|
||
|
</body>
|
||
|
</html>
|