Search code examples
restdarthttpserver

How to create Http Response with Dart


I was trying to follow along with one of the Dart HttpServer examples from GitHub, and, while it does show how to create and handle routes, I'm still at a loss on how to produce and send an HttpResponce in response to a specific url being requested.

For example, while working with WebAPI, I would have an Api Controller, in which I could explicitly define an action for GET, PUT, DELETE and POST verbs, and return an appropriate HttpResponse with a resource from each such method.

Does anyone know how this typical CRUD business is done using Dart as an Http server?


Solution

  • Once you receive a HttpRequest you need to use the response attribute to answer.

    void sendOk(HttpRequest request, [content]) {
      request.response
        ..statusCode = HttpStatus.OK
        ..write(content)
        ..close()
        ;
    }