planety / prologue

Powerful and flexible web framework written in Nim
https://planety.github.io/prologue
Apache License 2.0
1.24k stars 47 forks source link

[Question] Does Context Request includes json body passed to requests? #235

Closed naveedhd closed 8 months ago

naveedhd commented 9 months ago

I am calling a POST request to a route with a request body (json) but the Context does not seem to have the request body or any helper associated to it.

Am I missing something or is it not implemented yet?

mrtlsc commented 8 months ago

This is how you get the json body:

import std/json

proc endpoint*(ctx: Context) {.async.} =
  let body = parseJson(ctx.request.body)

then let's say the body contains a key "email" with a value of type string, you can get it like this:

let email = body["email"].getStr()

PhilippMDoerner commented 8 months ago

I have in fact asked myself the same thing a couple years ago and wrote this self answered SO question to have sth to google in the future :smile:

naveedhd commented 8 months ago

ah right! I was looking at the request object and missed out on the body func