How do I read HTTP body while using web.l

2019-05-14 Thread C K Kashyap
Hi,
I'm trying to write a simple web tool and would like to parse the HTTP post
body. I tried to alter the example on the website of web.l
 to read json payload like
this -

(de greet (Greeting)

  (res-html "The hello world page" NIL NIL

 ( NIL (prin Greeting)) ) )

(dh ("/greeters/" @Name)

   (out "+out.txt" (prinl (readJson)))

   (let Name (ht:Pack @Name)

  (case Name

 ("bob" (greet "Hello world!"))

 ("pablo" (greet "Hola mundo!"))

 ("hans" (greet "Hallo Welt!"))

 ("sergey" (greet "п?~@иве?~B ми?~@!"))

 (T (httpStat 404 "Not found")) ) ) )


This results in endless read :(. What's the right way to read the HTTP body?

Regards,

Kashyap


unsubscribe

2019-05-14 Thread Kuba Tyszko
Good bye Kuba Tyszko  :-(
You are now unsubscribed



-- 
UNSUBSCRIBE: mailto:picolisp@software-lab.de?subject=Unsubscribe


Re: How do I read HTTP body while using web.l

2019-05-14 Thread andreas

Hi Kashyap

In web.l framework, you you define a request handler using (dh).
When a http request is received by the (server) function (in 
web.l/http.l), it gets handed to the (http) function (defined in 
web.l/http.l).
Now (http) is parsing the complete HTTP request before calling 
(req-handler) at the end (defined in web.l/web.l), which in turn tries 
to find a suitable request-handler as defined with (dh) in the 
application code.


So when you do (readJson) within the request handler, (readJson) is 
reading from stdin, which here is the http connection socket, but the 
stdin/socket buffer was already fully read by (http), the client is 
waiting for an answer and not sending anymore, your (readJson) also 
waiting for input on the same connection, forever (logical dead-lock).


Data sent from the client using HTTP POST (so in the body) gets parsed 
by the web.l/http.l and put into an association list, where it can be 
fetched from using (get-form "argument-name").
If the uploaded data was a file (using multipart/form-data encoding), 
then you get from (get-form) the filepath to a temporary file where the 
content is stored.


So the solution (untested) should probably be something like:

(dh ("/upload-json-form")
   (ifn (= *ReqMethod 'POST) # if it is not POST, we assume GET, so some 
html should be delivered to the client

  ( NIL "/upload-json-form" T
  ( "file" "uploaded-file") )
  (in (get-form "uploaded-file") # switching input channel from 
client socket to uploaded file
 (out NIL # switching output channel to stdout of the 
application - we want to print our debug messages to the server console 
(or logfile), not into the HTTP client connection

 (prinl (readJson) ) ) ) )

Kind regards,
beneroth

Am 2019-05-14 19:59, schrieb C K Kashyap:

Hi,
I'm trying to write a simple web tool and would like to parse the HTTP
post body. I tried to alter the example on the website of webl [1] to
read json payload like this -

(de greet (Greeting)

  (res-html "The hello world page" NIL NIL

 ( NIL (prin Greeting)) ) )

(dh ("/greeters/" @Name)

   (out "+out.txt" (prinl (readJson)))

   (let Name (ht:Pack @Name)

  (case Name

 ("bob" (greet "Hello world!"))

 ("pablo" (greet "Hola mundo!"))

 ("hans" (greet "Hallo Welt!"))

 ("sergey" (greet "п?~@иве?~B ми?~@!"))

 (T (httpStat 404 "Not found")) ) ) )

This results in endless read :(. What's the right way to read the HTTP
body?

Regards,

Kashyap

Links:
--
[1] https://bitbucket.org/iromero91/web.l/wiki/Home


--
UNSUBSCRIBE: mailto:picolisp@software-lab.de?subject=Unsubscribe