Dear All,
I'm relatively new in Nim, came from Python. Developing my own little resource monitoring project and chose raw httpbeast as a platform for REST API due to its simpleness and performance.
I'm stuck with the way on how to send headers from server to client. To be honest, this is not important for the project itself, but unfortunately it's not documented. Maybe it will be useful for other beginners.
In httpbeast.nim:
proc send*(req: Request, code: HttpCode, body: string, headers="") =
{proc body}
No type mentioned for headers, and also
let myheaders = @["{'MyHeader:test'}"]
req.send("body string", headers=myheaders)
in httpbeast main app onRequest proc won't compile, as well as just assign an regular string to headers argument.
There are 3 pairs of arguments httpbeast sends to client by default : Content-Length, Server and Date
Thanks in advance
@demetera, Nim is statically typed and has some type inference, so when you don't see a type listed, Nim's type inference is at work. The type of headers is a string, because Nim infers the type from the default value. headers:string="" could also be used equivalently.
As for how to use that proc, you'll have to look at how it's used in Jester. You can see here on how to create the headers parameter.
So basic usage would be:
import strutils
import options
import httpbeast, asyncdispatch
import httpcore
type
RawHeaders = seq[tuple[key, val: string]]
proc createHeaders(headers: RawHeaders): string =
if headers.len > 0:
var res: seq[string]
for header in headers:
let (key, value) = header
res.add(key & ": " & value)
return res.join("\c\L")
proc onRequest(req: Request): Future[void] =
let headers = @({"Myheader": "test"})
if req.httpMethod == some(HttpGet):
case req.path.get()
of "/":
req.send(HTTP200, "Hello!", headers=createHeaders(headers))
else:
req.send(Http404)
run(onRequest)