This code is pretty simple and is a example, but is painfully slow for big files (10Mb)!
How to read data chunks of request body to a temp file instead of reading everything at once to memory?
import asynchttpserver, asyncdispatch
proc handler(req: Request) {.async.} =
echo req.url.path
if req.reqMethod == HttpPost:
echo req.headers
echo req.body.len
echo req.body
var form = """
<html>
<body>
<form action="/server" method="post" enctype="multipart/form-data">
<input type="file" name="textfile" accept="text/*"><br />
<input type="submit">
</form>
</body>
</html>
"""
await req.respond(Http200, form)
var server = newAsyncHttpServer(maxBody=16777216)
waitFor server.serve(Port(8080), handler)
Sadly this in a bit of state of flux.
I created something called FutureStream which can be used for this (but needs changes in asynchttpserver: the body should be a FutureStream just like HttpClient's request object's body is also a FutureStream). The FutureStream has issues though and we plan to redesign it, see https://github.com/nim-lang/Nim/issues/7126.
Any help in fixing #7126 is appreciated (by anyone).
I modified Dominik Picheta asynchttpserver library to try decode the post multipart request's. I'm a newbie in Nim programming, but the result can be seen here: https://github.com/mrhdias/EnigmaHTTPServer
At this point it is possible to decode multipart/data, x-form-urlencoded and ajax http post requests.