mirror of
https://github.com/aantron/dream.git
synced 2025-12-31 00:03:52 -05: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!
d-formparses request bodies as forms.e-jsonparses bodies as JSON.g-uploadreceives file upload forms.i-graphqlreceives GraphQL queries.j-streamstreams huge bodies.
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-templatebuilds responses from templates and guards against injection attacks (XSS).8-debugrenders error information in responses.