Hello there.
I have a problem about retrieve JSON values that posted from html form. This is part of my code to process POST action.
post "/login":
#do something here
let usr = parseJson(@"username")
let psd = parseJson(@"password")
resp "I Got Data : " & $usr & $psd
If I submit my form, i get this feedback
An error has occured in one of your routes.
Detail: Traceback (most recent call last)
webapp.nim(30) webapp
asyncdispatch.nim(1676) runForever
asyncdispatch.nim(1089) poll
asyncdispatch.nim(415) processPendingCallbacks
asyncdispatch.nim(1291) cb
asynchttpserver.nim(188) processClientIter
jester.nim(341) :anonymous
jester.nim(312) handleHTTPRequest
asyncdispatch.nim(1304) handleRequest
asyncdispatch.nim(1291) cb
jester.nim(273) handleRequestIter
asyncdispatch.nim(1304) match
asyncdispatch.nim(1291) cb
jester.nim(468) matchIter
json.nim(1150) parseJson
json.nim(1146) parseJson
json.nim(1136) parseJson
json.nim(593) raiseParseErr
input(1, 3) Error: { expected
Can you tell me what I miss?
Thank you.
ps : my NIM version is 0.14.2
If you have a html form it will most likely not being encoded as json. But even if it where it would be the request body which contained the data and not single variables.
I think you should try to echo @"username" and @"password" without the parseJson(). I guess that will be what you want (if jester parses the application/x-www-form-urlencoded data of the post into the params that is.
Thank you, OderWat.
Yes, my code work fine if without parseJson() like your suggestion. I need my nim webapp can read json value. Here is my html form.
<html>
<form method="POST" action="http://127.0.0.1:5050/login" enctype="application/json">
<input type="text" name="username"></input>
<input type="password" name="password"></input>
<input type="submit" name="submit" value="Login"></input>
</form>
</html>
Is there something wrong on html form?
Can you give some references about json parsing in Nim?
Thanks in advance.
application/json is not a supported enctype in HTML. See MDN documentation. There was a proposal for it - note the giant yellow-dotted banner there. It is not actively maintained and as far as I know, no browser supports it. So your problem is more on the client-side than on the server-side. But even if browsers would support it, this would need to be implemented in Jester, because when the parameters reach your own code, the request has already been parsed.
To sum up: Not possible because neither supported on the client nor on the server side. Review your requirements.
Hi, flyx.
Thank you for your references, that was help me.
I think i should to close this topic.