Skip to content

Response

The response can take various forms, including strings, maps, models, serializers, lists of models, and various types of exceptions.

/// string
class BlogController {
    index(DoxRequest req) {
        return 'pong';
    }
}
/// exception
class BlogController {
    index(DoxRequest req) {
        return InternalErrorException();
    }
}
/// map
class BlogController {
    index(DoxRequest req) {
        return {"message" : "hello world!"};
    }
}
/// Model
class BlogController {
    index(DoxRequest req) {
        Admin admin = await Admin().find(1)
        return admin;
    }
}
/// List<Model>
class BlogController {
    index(DoxRequest req) {
        List admins = await Admin().all()
        return admins;
    }
}
/// File: streamed, with Content-Length and Range support
class VideoController {
    show(DoxRequest req) {
        return File('storage/video.mp4');
    }
}
/// DownloadableFile
class PdfController {
    download(DoxRequest req) async {
        DownloadableFile file = await Storage().download('filename.pdf');

        return file;
    }
}
/// StreamFile
class PdfController {
    download(DoxRequest req) async {
        StreamFile file = await Storage().stream('filename.pdf');

        return file;
    }
}

Info

Models and lists of models will be automatically converted into JSON format and arrays of JSON.

Files and big responses

Return a File or a Stream<List<int>>. Dox pipes it to the client. It does not load it all into memory.

  • A File sets Content-Type (from the file name), Content-Length and Accept-Ranges.
  • It answers Range: bytes=... with 206 Partial Content. Video players can seek, and downloads can resume.
  • Storage().stream() and Storage().download() on the local disk also stream the file.
Route.get('/video', (DoxRequest req) => File('storage/video.mp4'));

With

Headers

return response()
    .header('Authorization', 'Bearer xxxxxx')
    .header('ContentType', 'application/json');
return response().withHeaders({
    'Authorization' : 'Bearer xxxxxx',
    'ContentType': 'application/json'
});

Status code

return response().statusCode(401);

Content type

return response().contentType(ContentType.json);

Stream

StreamFile file = await Storage().stream('filename.jpg');

return response().stream(file.stream).contentType(file.contentType);
var cookie = DoxCookie('key', 'value');
return response().cookie(cookie);
var cookie = DoxCookie(
    'token',
    'value',
    maxAge: Duration(days: 7), // sent as Max-Age in seconds. default 1 hour
    httpOnly: true,
    secure: true,
    path: '/',
);
return response().cookie(cookie);
controllerMethod(DoxRequest req) {
    cookie = req.cookie(key)
}

Encrypt / Decrypt

By default, a cookie's value is encrypted and decrypted using the APP_KEY. You can opt to disable encryption by setting encrypt: false when setting a cookie and disable decryption by setting decrypt: false when retrieving a cookie, eg. req.cookie(key, decrypt: false).