-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathechoheader_server.nim
More file actions
28 lines (25 loc) · 846 Bytes
/
echoheader_server.nim
File metadata and controls
28 lines (25 loc) · 846 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
import asynchttpserver, asyncdispatch
from strutils import join
from tables import `$`, pairs
from json import `$`, `[]=`, newJObject, newJString
var server = newAsyncHttpServer()
proc cb(req: Request) {.async.} =
var content = newJObject()
echo "method: ", $req.reqMethod
echo "hostname: ", req.hostname
echo "url: ", req.url
echo "body: ", req.body
echo "protocol: ", req.protocol
content["method"] = ($req.reqMethod).newJString
echo "filling headers"
for header, value in req.headers.table.pairs:
echo header, ": ", value
content[header] = value.join(", ").newJString
let msg = $content
echo "json content: ", msg
let headers = newHttpHeaders([
("Content-Type", "application/json")
])
await req.respond(Http200, msg, headers)
echo "server is waiting at port 3000"
waitFor server.serve(Port 3000, cb)