2013-11-13 17:01:15 +00:00
# Request
A Koa `Request` object is an abstraction on top of node's vanilla request object,
providing additional functionality that is useful for every day HTTP server
development.
## API
### req.header
Request header object.
### req.method
Request method.
### req.method=
Set request method, useful for implementing middleware
such as `methodOverride()` .
### req.length
2013-11-20 06:40:52 +00:00
Return request Content-Length as a number when present, or `undefined` .
2013-11-13 17:01:15 +00:00
### req.url
Get request URL.
### req.url=
Set request URL, useful for url rewrites.
2014-05-06 02:01:34 +00:00
### req.originalUrl
Get request original URL.
2013-11-13 17:01:15 +00:00
### req.path
Get request pathname.
### req.path=
Set request pathname and retain query-string when present.
2013-12-18 07:53:58 +00:00
### req.querystring
Get raw query string void of `?` .
### req.querystring=
Set raw query string.
### req.search
Get raw query string with the `?` .
### req.search=
Set raw query string.
### req.host
2014-05-05 15:44:00 +00:00
Get host (hostname:port) when present. Supports `X-Forwarded-Host`
2013-12-18 07:53:58 +00:00
when `app.proxy` is __true__ , otherwise `Host` is used.
2014-05-04 14:43:58 +00:00
### req.hostname
2014-05-05 15:44:00 +00:00
2014-05-04 14:43:58 +00:00
Get hostname when present. Supports `X-Forwarded-Host`
when `app.proxy` is __true__ , otherwise `Host` is used.
2014-01-07 19:01:11 +00:00
2013-12-18 07:53:58 +00:00
### req.type
Get request `Content-Type` void of parameters such as "charset".
```js
2014-02-14 17:40:58 +00:00
var ct = this.request.type;
2013-12-18 07:53:58 +00:00
// => "image/png"
```
2014-04-11 22:29:33 +00:00
### req.charset
2014-02-14 17:38:59 +00:00
Get request charset when present, or `undefined` :
```js
2014-02-14 17:40:58 +00:00
this.request.charset
2014-02-14 17:38:59 +00:00
// => "utf-8"
```
2013-11-13 17:01:15 +00:00
### req.query
2013-11-20 06:40:52 +00:00
2013-11-13 17:01:15 +00:00
Get parsed query-string, returning an empty object when no
query-string is present. Note that this getter does _not_
support nested parsing.
For example "color=blue& size=small":
```js
{
color: 'blue',
size: 'small'
}
```
### req.query=
Set query-string to the given object. Note that this
setter does _not_ support nested objects.
```js
this.query = { next: '/login' };
```
### req.fresh
Check if a request cache is "fresh", aka the contents have not changed. This
method is for cache negotiation between `If-None-Match` / `ETag` , and `If-Modified-Since` and `Last-Modified` . It should be referenced after setting one or more of these response headers.
```js
this.set('ETag', '123');
// cache is ok
if (this.fresh) {
this.status = 304;
return;
2013-11-20 06:40:52 +00:00
}
2013-11-13 17:01:15 +00:00
// cache is stale
// fetch new data
this.body = yield db.find('something');
```
### req.stale
Inverse of `req.fresh` .
### req.protocol
Return request protocol, "https" or "http". Supports `X-Forwarded-Proto`
when `app.proxy` is __true__ .
### req.secure
2013-12-19 07:47:48 +00:00
Shorthand for `this.protocol == "https"` to check if a request was
2013-11-13 17:01:15 +00:00
issued via TLS.
### req.ip
Request remote address. Supports `X-Forwarded-For` when `app.proxy`
is __true__ .
### req.ips
When `X-Forwarded-For` is present and `app.proxy` is enabled an array
of these ips is returned, ordered from upstream -> downstream. When disabled
an empty array is returned.
### req.subdomains
Return subdomains as an array.
2013-11-20 06:40:52 +00:00
2013-11-13 17:01:15 +00:00
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 `app.subdomainOffset` .
2013-11-20 06:40:52 +00:00
2013-11-13 17:01:15 +00:00
For example, if the domain is "tobi.ferrets.example.com":
If `app.subdomainOffset` is not set, this.subdomains is `["ferrets", "tobi"]` .
If `app.subdomainOffset` is 3, this.subdomains is `["tobi"]` .
### req.is(type)
2013-11-20 06:40:52 +00:00
2013-11-28 08:13:13 +00:00
Check if the incoming request contains the "Content-Type"
header field, and it contains any of the give mime `type` s.
2013-11-28 20:47:23 +00:00
If there is no request body, `undefined` is returned.
If there is no content type, or the match fails `false` is returned.
Otherwise, it returns the matching content-type.
2013-11-20 06:40:52 +00:00
```js
2013-11-13 17:01:15 +00:00
// With Content-Type: text/html; charset=utf-8
2013-11-28 08:13:13 +00:00
this.is('html'); // => 'html'
this.is('text/html'); // => 'text/html'
2013-11-28 20:47:23 +00:00
this.is('text/*', 'text/html'); // => 'text/html'
2013-11-13 17:01:15 +00:00
// When Content-Type is application/json
2013-11-28 08:13:13 +00:00
this.is('json', 'urlencoded'); // => 'json'
this.is('application/json'); // => 'application/json'
2013-11-28 20:47:23 +00:00
this.is('html', 'application/*'); // => 'application/json'
2013-11-28 08:13:13 +00:00
this.is('html'); // => false
2013-11-28 20:47:23 +00:00
```
For example if you want to ensure that
only images are sent to a given route:
```js
if (this.is('image/*')) {
// process
} else {
this.throw(415, 'images only!');
}
2013-11-13 17:01:15 +00:00
```
2014-01-18 22:22:53 +00:00
### Content Negotiation
Koa's `request` object includes helpful content negotiation utilities powered by [accepts ](http://github.com/expressjs/accepts ) and [negotiator ](https://github.com/federomero/negotiator ). These utilities are:
- `req.accepts(types)`
- `req.acceptsEncodings(types)`
- `req.acceptsCharsets(charsets)`
- `req.acceptsLanguages(langs)`
If no types are supplied, __all__ acceptable types are returned.
2013-11-13 17:01:15 +00:00
2014-01-18 22:22:53 +00:00
If multiple types are supplied, the best match will be returned. If no matches are found, a `false` is returned, and you should send a `406 "Not Acceptable"` response to the client.
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.
### req.accepts(types)
2013-11-20 06:40:52 +00:00
2014-01-18 22:22:53 +00:00
Check if the given `type(s)` is acceptable, returning the best match when true, otherwise `false` . The `type` value may be one or more mime type string
2013-11-13 17:01:15 +00:00
such as "application/json", the extension name
2013-11-21 02:00:24 +00:00
such as "json", or an array `["json", "html", "text/plain"]` .
2013-11-20 06:40:52 +00:00
```js
2013-11-13 17:01:15 +00:00
// Accept: text/html
this.accepts('html');
// => "html"
// Accept: text/*, application/json
this.accepts('html');
// => "html"
this.accepts('text/html');
// => "text/html"
this.accepts('json', 'text');
// => "json"
this.accepts('application/json');
// => "application/json"
// Accept: text/*, application/json
this.accepts('image/png');
this.accepts('png');
2014-01-18 22:22:53 +00:00
// => false
2013-11-13 17:01:15 +00:00
// Accept: text/*;q=.5, application/json
this.accepts(['html', 'json']);
this.accepts('html', 'json');
// => "json"
2014-01-18 22:22:53 +00:00
// No Accept header
this.accepts('html', 'json');
// => "html"
this.accepts('json', 'html');
// => "json"
2013-11-13 17:01:15 +00:00
```
You may call `this.accepts()` as may times as you like,
or use a switch:
```js
switch (this.accepts('json', 'html', 'text')) {
case 'json': break;
case 'html': break;
case 'text': break;
2014-01-18 22:22:53 +00:00
default: this.throw(406, 'json, html, or text only');
2013-11-13 17:01:15 +00:00
}
```
### req.acceptsEncodings(encodings)
2014-01-18 22:22:53 +00:00
Check if `encodings` are acceptable, returning the best match when true, otherwise `false` . Note that you should include `identity` as one of the encodings!
2013-11-13 17:01:15 +00:00
```js
// Accept-Encoding: gzip
2014-01-18 22:22:53 +00:00
this.acceptsEncodings('gzip', 'deflate', 'identity');
2013-11-13 17:01:15 +00:00
// => "gzip"
2014-01-18 22:22:53 +00:00
this.acceptsEncodings(['gzip', 'deflate', 'identity']);
2013-11-13 17:01:15 +00:00
// => "gzip"
```
When no arguments are given all accepted encodings
are returned as an array:
```js
// Accept-Encoding: gzip, deflate
this.acceptsEncodings();
2014-01-18 22:22:53 +00:00
// => ["gzip", "deflate", "identity"]
2013-11-13 17:01:15 +00:00
```
2014-01-18 22:22:53 +00:00
Note that the `identity` encoding (which means no encoding) could be unacceptable if the client explicitly sends `identity;q=0` . Although this is an edge case, you should still handle the case where this method returns `false` .
2013-11-13 17:01:15 +00:00
### req.acceptsCharsets(charsets)
Check if `charsets` are acceptable, returning
2014-01-18 22:22:53 +00:00
the best match when true, otherwise `false` .
2013-11-13 17:01:15 +00:00
```js
// Accept-Charset: utf-8, iso-8859-1;q=0.2, utf-7;q=0.5
this.acceptsCharsets('utf-8', 'utf-7');
// => "utf-8"
this.acceptsCharsets(['utf-7', 'utf-8']);
// => "utf-8"
```
When no arguments are given all accepted charsets
are returned as an array:
```js
// Accept-Charset: utf-8, iso-8859-1;q=0.2, utf-7;q=0.5
this.acceptsCharsets();
// => ["utf-8", "utf-7", "iso-8859-1"]
```
### req.acceptsLanguages(langs)
Check if `langs` are acceptable, returning
2014-01-18 22:22:53 +00:00
the best match when true, otherwise `false` .
2013-11-13 17:01:15 +00:00
```js
// Accept-Language: en;q=0.8, es, pt
this.acceptsLanguages('es', 'en');
// => "es"
this.acceptsLanguages(['en', 'es']);
// => "es"
```
When no arguments are given all accepted languages
are returned as an array:
```js
// Accept-Language: en;q=0.8, es, pt
this.acceptsLanguages();
// => ["es", "pt", "en"]
```
2014-01-17 08:30:35 +00:00
### req.idempotent
Check if the request is idempotent.
### req.socket
Return the request socket.
### req.get(field)
Return request header.