Skip to content

Commit

Permalink
improvement: document missing request methods
Browse files Browse the repository at this point in the history
  • Loading branch information
thetutlage committed May 13, 2021
1 parent 8fb23d3 commit 9e7f8a9
Showing 1 changed file with 104 additions and 4 deletions.
108 changes: 104 additions & 4 deletions content/guides/http/request.md
Original file line number Diff line number Diff line change
Expand Up @@ -394,7 +394,7 @@ Form method spoofing only works:

The client making the request can negotiate for the **resource representation**, **charset**, **language**, and **encoding** using different `Accept` headers, and you can handle them as follows.

### request.accepts
### accepts

The `request.accepts` method takes an array of content types (including shorthands) and returns the most appropriate content type by inspecting the `Accept` header. You can find the list of supported content types [here](https://github.com/jshttp/mime-db/blob/master/db.json).

Expand All @@ -417,7 +417,18 @@ Route.get('posts', async ({ request, view }) => {
})
```

### request.language
---

### types
The `request.types` method returns an array of content types by inspecting the `Accept` header. The array is ordered by the client's preference (most preferred first).

```ts
const types = request.types()
```

---

### language

Negotiate for the requested language based upon the `Accept-language` header.

Expand All @@ -431,7 +442,19 @@ if (language) {
return view.render('posts/en/index')
```

### request.encoding
---

### languages

The `languages` method returns an array of accepted languages by inspecting the `Accept-language` header. The array is ordered by the client's preference (most preferred first).

```ts
const languages = request.languages()
```

---

### encoding

Find the best encoding using the `Accept-encoding` header.

Expand All @@ -446,7 +469,18 @@ switch (request.encoding(['gzip', 'br'])) {
}
```

### request.charset
---

### encodings
The `encodings` method returns an array of accepted encoding by inspecting the `Accept-encoding` header. The array is ordered by the client's preference (most preferred first).

```ts
const encodings = request.encodings()
```

---

### charset

Find the best charset using the `Accept-charset` header.

Expand All @@ -455,6 +489,15 @@ const charset = request.charset(['utf-8', 'hex', 'ascii'])
return Buffer.from('hello-world').toString(charset || 'utf-8')
```

---

### charset
The `charset` method returns an array of accepted charsets by inspecting the `Accept-charset` header. The array is ordered by the client's preference (most preferred first).

```ts
const charsets = request.charsets()
```

## Trusted proxy

The majority of Node.js applications are deployed behind a proxy server like Nginx or Caddy. Hence, the value of [remoteAddress](https://nodejs.org/api/net.html#net_socket_remoteaddress) is the IP address of the proxy server and not the client.
Expand Down Expand Up @@ -541,6 +584,62 @@ The config file is extensively documented. Make sure to go through all the optio

Following is the list of other available methods and properties on the Request class.

### hostname
Returns the request hostname. If [proxy headers](#trusted-proxy) are trusted, then `X-Forwarded-Host` is given priority over the `Host` header.

```ts
request.hostname()
```

---

### ajax
Find if the request header `X-Requested-With` is set to `'xmlhttprequest'`.

```ts
if (request.ajax()) {
// return response for ajax request
}
```

---

### matchesRoute
Find if the current request is for a given route. The method accepts the route identifier as the only argument. The identifier can be the **route pattern**, **controller.method name** or the **route name**.

```ts
if (request.matchesRoute('posts.show')) {
}
```

You can also match against the multiple routes. The method returns `true` if the returns URL matches any of the defined identifiers.

```ts
if (request.matchesRoute(['posts.show', 'posts.edit'])) {
}
```

---

### is
Returns the best matching content type of the request by matching against the given types.

The content type is picked from the `Content-Type` header and request must have body.

```ts
const contentType = request.is(['json', 'xml'])

if (contentType === 'json') {
// process body as JSON
}

if (contentType === 'xml') {
// process body as XML
}
```

---

### updateBody

Allows you to update the request body with a custom payload. It would be best if you weren't doing it unless creating a package that purposefully mutates the request body.
Expand Down Expand Up @@ -643,5 +742,6 @@ declare module '@ioc:Adonis/Core/Request' {
Following are some of the additional guides to learn more about the topics not covered in this document.

- [Cookies](./cookies.md)
- [Signed URLs](../security/signed-urls.md)
- [File uploads](./file-uploads.md)
- [Validations](../validator/introduction.md)

0 comments on commit 9e7f8a9

Please sign in to comment.