2021-04-06 18:48:47 +03:00
..
2021-04-05 01:10:11 +03:00
2021-04-05 00:08:28 +03:00
2021-04-05 01:10:11 +03:00
2021-04-06 18:48:47 +03:00

6-echo


This example just echoes the bodies of POST /echo requests, after reading them with Dream.body:

let () =
  Dream.run
  @@ Dream.logger
  @@ Dream.router [
    Dream.post "/echo" (fun request ->
      let%lwt body = Dream.body request in
      Dream.respond body);
  ]
  @@ Dream.not_found
$ dune exec --root . ./echo.exe

You can test it with curl:

$ curl http://localhost:8080/echo --data foo
foo

...or try HTTPie:

$ echo -n foo | http POST :8080/echo
HTTP/1.1 200 OK
Content-Length: 3

foo

We usually want to do something more interesting with the request body than just echo it, and there are several examples for that!

We delay these examples a bit, so we can squeeze in a couple security topics first. These examples do take client input, after all! So, it's better to present them the right way.


Next steps:

  • 7-template builds responses from templates and guards against injection attacks (XSS).
  • 8-debug renders error information in responses.

Up to the tutorial index