Add respond proc example

Add an example for the respond proc demonstration JSON response.
This commit is contained in:
honewatson 2016-10-03 20:22:32 +11:00 • committed by GitHub
commit 065210e6a3

View file

@ -81,6 +81,17 @@ proc respond*(req: Request, code: HttpCode, content: string,
## content. ## content.
## ##
## This procedure will **not** close the client socket. ## This procedure will **not** close the client socket.
##
## Examples
## --------
## .. code-block::nim
## proc handler(req: Request) {.async.} =
## if $req.url.path == "/hello-world":
## let msg = %* {"message": "Hello World"}
## let headers = newHttpHeaders([("Content-Type","application/json")])
## await req.respond(Http200, $msg, headers)
## else:
## await req.respond(Http404, "Not Found")
var msg = "HTTP/1.1 " & $code & "\c\L" var msg = "HTTP/1.1 " & $code & "\c\L"
if headers != nil: if headers != nil: