You are viewing the legacy version of AdonisJS. Visit for newer docs. This version will receive security patches until the end of 2021.


This guide gives an outline on how to make use of the HTTP Response object to make the response.

The Node.js raw res object can be accessed as response.response.

AdonisJs passes the current HTTP response object as part of the context which is sent to all route handlers and middleware.

Route.get('/', ({ response }) => {
  response.send('hello world')

Basic example

Let’s see how to send an array of users back as JSON from the route.

Route.get('/', async ({ response }) => {
  const users = await User.all()

Also, you can use response.json method as an alias to response.send

Making response

As of 4.0, you can also return values from the route closure or controller method, instead of using dedicated response methods.

The following is equivalent to response.send or response.json but feels more natural with a return statement.

Route.get('/', async () => {
  return await User.all()

No callbacks please

Since the request/response lifecycle allows you to return values and calling dedicated methods, it discourages the usage of callbacks entirely.

Which means the following will not work.

Route.get('/', async ({ response }) => {
  fs.readFile('somefile', (error, contents) => {

The reason why above code is not going to work, is that as soon the route handler is executed, AdonisJs ends the response, and your callback gets executed later, resulting in an error.

It may sound scary, but it is done in favor of having a clean codebase, instead of sprinkling mixed API everywhere.

Promisfying callbacks

What you can do instead is promisify the callback and use it with await.

const Helpers = use('Helpers')
const fs = use('fs')
const readFile = Helpers.promisify(fs.readFile)

Route.get('/', async ({ response }) => {
  return await readFile('somefile')

See how cleaner the code is now. Javascript has a rich eco-system, and it is 100% possible to write code without callbacks just by promisfying them, and as a community, we want to encourage it.

I need callbacks

If you think using callbacks is great, we have a way for you to use them. Simply instruct the response object not to end implicitly.

Route.get('/', async ({ response }) => {
  response.implicitEnd = false

  fs.readFile('somefile', (error, contents) => {


Following is the list of methods to set/remove headers.


Set’s header on the response

response.header('Content-type', 'application/json')


Only set the header if it does not exist.

response.safeHeader('Content-type', 'application/json')


Remove existing header



Set Content-type header.



Also, you can set/remove cookies by calling one of the following methods

response.cookie('cartTotal', 20)


Remove existing cookie by setting the expiry in the past.



Since all cookies are encrypted and signed, it is not possible to read them from frontend Javascript code. In that case, may want to set a plain cookie instead.

// not signed or encrypted
response.plainCookie('cartTotal', 20)


Use one of the following methods to redirect requests to a different URL.

redirect(url, sendParams = false, status = 302)

Redirect request to a different url, by default it will set the status as 302.


// or
response.redirect('/url', false, 301)

Also, you can send current request params to the other request as well by setting true as the 2nd param.

response.redirect('/url', true)

route(route, data, domain, sendParams = false, status = 302)

Redirect to a route. This method builds the URL automatically from the route name or controller method.

Defining route
  .get('users/:id', '')
response.route('profile', { id: 1 })

Also, you can pass the controller method.

response.route('', { id: 1 })

Since AdonisJs allows registering routes for multiple domains, you can instruct this method to build the URL for a specific domain.

response.route('posts', { id: 1 }, '')


The response object makes it super easy to stream files from your server to the client.


Stream the file to the client. This method does not force the client to download the file as an attachment. For example, Browsers may choose to display the file in a new window.'uploads/avatar.jpg'))

attachment(filePath, name, disposition)

Force download the file


With a custom name


Extending Response

It is also possible to extend the Response prototype by adding your own methods, known as macros.

Application specific

If your macros are specific to your application only, you can add the macro inside start/hooks.js file after the providers have been booted.

const { hooks } = require('@adonisjs/ignitor')

hooks.after.providersBooted(() => {
  const Response = use('Adonis/Src/Response')

  Response.macro('sendStatus', function (status) {

Via Provider

If you are writing a module/addon for AdonisJs, you can add a macro inside the boot method of your service provider.

const { ServiceProvider } = require('@adonisjs/fold')

class MyServiceProvider extends ServiceProvider {
  boot () {
    const Response = use('Adonis/Src/Response')

    Response.macro('sendStatus', function (status) {