Re: A quick way to write an http endpoint to handle post body

2019-10-20 Thread George Orais
Hi Kashyap,
So cool! Thanks for sharing!

BR,Geo
On Friday, 13 September 2019, 12:45:44 am GMT+9, C K Kashyap 
 wrote:  
 
 Another update - I controlled my home made raspberry pi robot using the Bot 
Framework bot :)
Regards,Kashyap
On Mon, May 27, 2019 at 9:56 AM C K Kashyap  wrote:

Thanks Alex!

On Sat, May 25, 2019 at 12:06 AM Alexander Burger  wrote:

Hi Kashyap,

> I have it done finally. I've successfully built a "bot" that talks the
> "Azure Bot" protocol -
> Please take a look at http://ckkvm.westus2.cloudapp.azure.com:8080/ which
> presents a chat UI. It's a glorified REPL at this point :)

Wow, this looks cool!


> I've attache the bot code. The protocol is really simple. Messages are
> posted to the bot via an HTTP POST with the message in the body as a JSON

Re: A quick way to write an http endpoint to handle post body

2019-09-13 Thread C K Kashyap
Thanks :)

On Thu, Sep 12, 2019 at 8:59 AM Alexander Burger 
wrote:

> Hi Kashyap,
>
> > Another update - I controlled my home made raspberry pi robot using the
> Bot
> > Framework  bot :)
>
> Cute! Very nice! :)
> ☺/ A!ex
>
> --
> UNSUBSCRIBE: mailto:picolisp@software-lab.de?subject=Unsubscribe
>


Re: A quick way to write an http endpoint to handle post body

2019-09-12 Thread Alexander Burger
Hi Kashyap,

> Another update - I controlled my home made raspberry pi robot using the Bot
> Framework  bot :)

Cute! Very nice! :)
☺/ A!ex

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


Re: A quick way to write an http endpoint to handle post body

2019-05-27 Thread C K Kashyap
Thanks Alex!

On Sat, May 25, 2019 at 12:06 AM Alexander Burger 
wrote:

> Hi Kashyap,
>
> > I have it done finally. I've successfully built a "bot" that talks the
> > "Azure Bot" protocol -
> > Please take a look at http://ckkvm.westus2.cloudapp.azure.com:8080/
> which
> > presents a chat UI. It's a glorified REPL at this point :)
>
> Wow, this looks cool!
>
>
> > I've attache the bot code. The protocol is really simple. Messages are
> > posted to the bot via an HTTP POST with the message in the body as a
> JSON.
> > ...
>
> Nice project! Thanks!! :)
>
> ☺/ A!ex
>
> --
> UNSUBSCRIBE: mailto:picolisp@software-lab.de?subject=Unsubscribe
>


Re: A quick way to write an http endpoint to handle post body

2019-05-25 Thread Alexander Burger
Hi Kashyap,

> I have it done finally. I've successfully built a "bot" that talks the
> "Azure Bot" protocol -
> Please take a look at http://ckkvm.westus2.cloudapp.azure.com:8080/ which
> presents a chat UI. It's a glorified REPL at this point :)

Wow, this looks cool!


> I've attache the bot code. The protocol is really simple. Messages are
> posted to the bot via an HTTP POST with the message in the body as a JSON.
> ...

Nice project! Thanks!! :)

☺/ A!ex

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


Re: A quick way to write an http endpoint to handle post body

2019-05-24 Thread C K Kashyap
Okay,
I have it done finally. I've successfully built a "bot" that talks the
"Azure Bot" protocol -
Please take a look at http://ckkvm.westus2.cloudapp.azure.com:8080/ which
presents a chat UI. It's a glorified REPL at this point :)
I've attache the bot code. The protocol is really simple. Messages are
posted to the bot via an HTTP POST with the message in the body as a JSON.
The header contains a token but my implementation ignores the token.
Responses are posted back and a token needs to be generated for that using
an APP_ID and PASSWORD.
I had to give up on json.l because it looks like printJson does not handle
array too well. I cooked up a manual parsing logic and it does not look
pretty :)

I am reading McCarthy's history of Lisp currently and hopefully will be
able to figure out some neat trick to do some AI stuff that Lisp lends
itself to more easily that other languages

[image: Capture.PNG]

On Sun, May 19, 2019 at 1:07 AM Alexander Burger 
wrote:

> Hi Mansur,
>
> > finally I've found it
>
> Cool, thanks! :)
>
>
> Minor note:
>
> > (push '*Mimes '(`(chop "json") "application/json; charset=utf-8"))
>
> There is a utility 'mime' for that, also in @lib/http.l
>
>(mime "json" "application/json; charset=utf-8" 1)
>
> It not only pushes new entries, but also modifies existing
> entries in '*Mimes'. '1' is the (optional) max-age.
>
> ☺/ A!ex
>
> --
> UNSUBSCRIBE: mailto:picolisp@software-lab.de?subject=Unsubscribe
>


bot.tgz
Description: application/compressed


Re: A quick way to write an http endpoint to handle post body

2019-05-19 Thread Alexander Burger
Hi Mansur,

> finally I've found it

Cool, thanks! :)


Minor note:

> (push '*Mimes '(`(chop "json") "application/json; charset=utf-8"))

There is a utility 'mime' for that, also in @lib/http.l

   (mime "json" "application/json; charset=utf-8" 1)

It not only pushes new entries, but also modifies existing
entries in '*Mimes'. '1' is the (optional) max-age.

☺/ A!ex

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


Re: A quick way to write an http endpoint to handle post body

2019-05-19 Thread Мансур Мамкин
Hi again,
finally I've found it
Sorry, I was in hurry and sent it to wrong place first time :)

The same at pastebin: https://pastebin.com/nQVqbrUz

#
# Mansur Mamkin 
# httpjson.l adds support for POST-requests with JSON body
#

(push '*Mimes '(`(chop "json") "application/json; charset=utf-8"))

(de _htJson ()
   (let F (tmp "json.json")
  (out F (echo *ContLen))
  (in F (setq *JSON (readJson T))) ) )

(patch http
'(cond @A ((=0 *ContLen)) @B)
(fill
 '(cond
^ @A
((=0 *ContLen))
((pre? "application/json" *ContentType) (_htJson))
^ @B ) ) )


(patch _htHead
'("content-type:" @A)
'("content-type:"
(char) # or better (skip)?
(case (lowc (till "/" T))
   ("multipart"
  (if (= "/form-data; boundary" (lowc (till "=" T)))
 (setq
*ContentType "multipart/form-data"
*MPartLim (append '(- -) (cdr (line)))
*MPartEnd (append *MPartLim '(- -)) )
 (line) ) )
   (T (setq *ContentType (pack @ (line T))) ) ) ) )

#example
#{
(allow "!getjson")

(de getjson ()
   (msg 'JSON= (sym *JSON))
   (let F (tmp "out.json")
  (out F (printJson '(("answerParam" . "ok"
  (httpEcho F "application/json; charset=utf-8" 1) ) )

}#

Best regards,
Mansur Mamkin

On Sat, May 18, 2019 at 8:29 PM C K Kashyap  wrote:

On Sat, May 18, 2019 at 8:29 PM C K Kashyap  wrote:

> Hi Alex et al,
>
> I need a sample to handle HTTP post. All I need to do is listen for HTTP
> post request, read the payload in the body and respond. Before you get
> tempted to say a variation of RTFM - let me say that I promise to share an
> interesting demo once I have that :)
>
> Regards,
> Kashyap
>
>


Re: A quick way to write an http endpoint to handle post body

2019-05-19 Thread Мансур Мамкин
HI all!
There is another way, you can make a little patch in http.l in standard
library to handle 'application/json' mimetype in POST body.
I've tried to find that implementation in my archive, but no success at the
moment. I hope I'll find later today

On Sat, May 18, 2019 at 8:29 PM C K Kashyap  wrote:

> Hi Alex et al,
>
> I need a sample to handle HTTP post. All I need to do is listen for HTTP
> post request, read the payload in the body and respond. Before you get
> tempted to say a variation of RTFM - let me say that I promise to share an
> interesting demo once I have that :)
>
> Regards,
> Kashyap
>
>


Re: A quick way to write an http endpoint to handle post body

2019-05-18 Thread Alexander Burger
On Sat, May 18, 2019 at 04:39:07PM -0700, C K Kashyap wrote:
> Okay .. this works for me -

Great :)

Yes, a pipe seems the best way here, especially if you can put the main work
into it to distribute the CPU load.

☺/ A!ex

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


Re: A quick way to write an http endpoint to handle post body

2019-05-18 Thread C K Kashyap
Okay .. this works for me -

(setq *Port (port 3000))


(de process (Input) (if Input Input "Empty"))


(loop

 (setq *Sock (listen *Port))

 (unless (fork)

  (close *Port)

  (let Body (pipe

   (in *Sock

(when (= "POST" (read))

 (when (from "Content-Length: ")

  (let Len (read)

   (line) # read the newline after content length

   (while (line))  # Skip rest of header

   (echo Len)  

   (till NIL T))


   (let Output (process Body)

(out *Sock

 (prinl "HTTP/1.1 200 OK^M")

 (prinl "Content-Length: " (length Output) "^M^J^M")

 (prinl Output)))

  (bye) ))

 (close *Sock) )


Regards,

Kashyap

On Sat, May 18, 2019 at 3:43 PM C K Kashyap  wrote:

> Super! ... almost what I was looking for  I mean one minor fix is that
> I have to call a (line) after (read) to read up the newline after the
> content length.
> Also, I need to figure out how to capture the body  do I do a pipe?
> Regards,
> Kashyap
>
> On Sat, May 18, 2019 at 11:53 AM Alexander Burger 
> wrote:
>
>> On Sat, May 18, 2019 at 08:11:20PM +0200, Alexander Burger wrote:
>> > You can easily write a stand-alone server without any library:
>> > ...
>>
>> Hmm, this was too short. I haven't tested, but at least we should respond
>> something. Perhaps this is a better starting point:
>>
>>(setq *Port (port 8080))
>>
>>(loop
>>   (setq *Sock (listen *Port))
>>   (unless (fork)
>>  (close *Port)
>>  (in *Sock
>> (when (from "Content-Length: ")
>>(let Len (read)
>>   (while (line))  # Skip rest of header
>>   (out NIL (echo Len)) ) ) )
>>  (out *Sock
>> (prinl "HTTP/1.1 200 OK^M")
>> (prinl "Content-Length: 0^M^J^M")
>> (prinl "^M")
>> (prinl "^M") )
>>  (bye) )
>>   (close *Sock) )
>>
>> Just for experimentation ... ;)
>>
>> ☺/ A!ex
>>
>> --
>> UNSUBSCRIBE: mailto:picolisp@software-lab.de?subject=Unsubscribe
>>
>


Re: A quick way to write an http endpoint to handle post body

2019-05-18 Thread C K Kashyap
Super! ... almost what I was looking for  I mean one minor fix is that
I have to call a (line) after (read) to read up the newline after the
content length.
Also, I need to figure out how to capture the body  do I do a pipe?
Regards,
Kashyap

On Sat, May 18, 2019 at 11:53 AM Alexander Burger 
wrote:

> On Sat, May 18, 2019 at 08:11:20PM +0200, Alexander Burger wrote:
> > You can easily write a stand-alone server without any library:
> > ...
>
> Hmm, this was too short. I haven't tested, but at least we should respond
> something. Perhaps this is a better starting point:
>
>(setq *Port (port 8080))
>
>(loop
>   (setq *Sock (listen *Port))
>   (unless (fork)
>  (close *Port)
>  (in *Sock
> (when (from "Content-Length: ")
>(let Len (read)
>   (while (line))  # Skip rest of header
>   (out NIL (echo Len)) ) ) )
>  (out *Sock
> (prinl "HTTP/1.1 200 OK^M")
> (prinl "Content-Length: 0^M^J^M")
> (prinl "^M")
> (prinl "^M") )
>  (bye) )
>   (close *Sock) )
>
> Just for experimentation ... ;)
>
> ☺/ A!ex
>
> --
> UNSUBSCRIBE: mailto:picolisp@software-lab.de?subject=Unsubscribe
>


Re: A quick way to write an http endpoint to handle post body

2019-05-18 Thread Alexander Burger
On Sat, May 18, 2019 at 08:11:20PM +0200, Alexander Burger wrote:
> You can easily write a stand-alone server without any library:
> ...

Hmm, this was too short. I haven't tested, but at least we should respond
something. Perhaps this is a better starting point:

   (setq *Port (port 8080))

   (loop
  (setq *Sock (listen *Port))
  (unless (fork)
 (close *Port)
 (in *Sock
(when (from "Content-Length: ")
   (let Len (read)
  (while (line))  # Skip rest of header
  (out NIL (echo Len)) ) ) )
 (out *Sock
(prinl "HTTP/1.1 200 OK^M")
(prinl "Content-Length: 0^M^J^M")
(prinl "^M")
(prinl "^M") )
 (bye) )
  (close *Sock) )

Just for experimentation ... ;)

☺/ A!ex

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


Re: A quick way to write an http endpoint to handle post body

2019-05-18 Thread Alexander Burger
On Sat, May 18, 2019 at 10:18:42AM -0700, C K Kashyap wrote:
> Okay ... I think I can see how I can use web.l  One problem I am facing
> is to figure out how to read from *ContLen bytes from the input. What's a
> good way to read *ContLen bytes from the input? Using (char) in a loop does
> not seem right since it reads characters off the stream.

It depends where you want to put the data.

Writing to a file (or similarly to a pipe) is easy:

   (out "File" (echo *ContLen))


If you want them in a list or string, you may need 'char' in a loop

   (make (do *ContLen (link (char

but note that a char may be up to 4 bytes. Not really suitable.

Also easy, but rather useless, is reading it into a big number

   (rd *ContLen)  # Just kidding ;)

☺/ A!ex

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


Re: A quick way to write an http endpoint to handle post body

2019-05-18 Thread C K Kashyap
Okay ... I think I can see how I can use web.l  One problem I am facing
is to figure out how to read from *ContLen bytes from the input. What's a
good way to read *ContLen bytes from the input? Using (char) in a loop does
not seem right since it reads characters off the stream.

Regards,
Kashyap

On Sat, May 18, 2019 at 9:55 AM C K Kashyap  wrote:

> Thanks Alex,
> Although, I need a way to get raw access to the body (it happens to be
> json payload). I do not have control of the client code :(
> Regards,
> Kashyap
>
>
> On Sat, May 18, 2019 at 9:53 AM Alexander Burger 
> wrote:
>
>> Hi Kashyap,
>>
>> > I need a sample to handle HTTP post. All I need to do is listen for HTTP
>> > post request, read the payload in the body and respond.
>>
>> Perhaps web.l is more general and flexible, but as I said I haven't
>> studied it.
>> With the on-board server you could start this script:
>>
>>(load "@lib/http.l")
>>
>>(allowed ()
>>   "!work" *Var1 var2 )
>>
>>(de work ()
>>   (msg *Var1 " *Var1")
>>   (msg (get 'var2 'http) " var2")
>>   (respond) )
>>
>>(server 8080 "!work")
>>
>> It waits for HTTP requests. Now do in another terinal:
>>
>>$ curl -F "*Var1=val1" -F "var2=val2" http://localhost:8080
>>
>> The first terminal echoes:
>>
>>"val1" *Var1
>>"val2" var2
>>
>> ☺/ A!ex
>>
>> --
>> UNSUBSCRIBE: mailto:picolisp@software-lab.de?subject=Unsubscribe
>>
>


Re: A quick way to write an http endpoint to handle post body

2019-05-18 Thread C K Kashyap
Thanks Alex,
Although, I need a way to get raw access to the body (it happens to be json
payload). I do not have control of the client code :(
Regards,
Kashyap


On Sat, May 18, 2019 at 9:53 AM Alexander Burger 
wrote:

> Hi Kashyap,
>
> > I need a sample to handle HTTP post. All I need to do is listen for HTTP
> > post request, read the payload in the body and respond.
>
> Perhaps web.l is more general and flexible, but as I said I haven't
> studied it.
> With the on-board server you could start this script:
>
>(load "@lib/http.l")
>
>(allowed ()
>   "!work" *Var1 var2 )
>
>(de work ()
>   (msg *Var1 " *Var1")
>   (msg (get 'var2 'http) " var2")
>   (respond) )
>
>(server 8080 "!work")
>
> It waits for HTTP requests. Now do in another terinal:
>
>$ curl -F "*Var1=val1" -F "var2=val2" http://localhost:8080
>
> The first terminal echoes:
>
>"val1" *Var1
>"val2" var2
>
> ☺/ A!ex
>
> --
> UNSUBSCRIBE: mailto:picolisp@software-lab.de?subject=Unsubscribe
>


Re: A quick way to write an http endpoint to handle post body

2019-05-18 Thread Alexander Burger
Hi Kashyap,

> I need a sample to handle HTTP post. All I need to do is listen for HTTP
> post request, read the payload in the body and respond.

Perhaps web.l is more general and flexible, but as I said I haven't studied it.
With the on-board server you could start this script:

   (load "@lib/http.l")

   (allowed ()
  "!work" *Var1 var2 )

   (de work ()
  (msg *Var1 " *Var1")
  (msg (get 'var2 'http) " var2")
  (respond) )

   (server 8080 "!work")

It waits for HTTP requests. Now do in another terinal:

   $ curl -F "*Var1=val1" -F "var2=val2" http://localhost:8080

The first terminal echoes:

   "val1" *Var1
   "val2" var2

☺/ A!ex

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


A quick way to write an http endpoint to handle post body

2019-05-18 Thread C K Kashyap
Hi Alex et al,

I need a sample to handle HTTP post. All I need to do is listen for HTTP
post request, read the payload in the body and respond. Before you get
tempted to say a variation of RTFM - let me say that I promise to share an
interesting demo once I have that :)

Regards,
Kashyap