artanis
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Artanis] accept POST in "application" mode


From: Nala Ginrut
Subject: Re: [Artanis] accept POST in "application" mode
Date: Thu, 13 Sep 2018 01:47:05 +0800
User-agent: mu4e 1.1.0; emacs 25.1.1

Julio Claudio Matus Ramirez writes:

> Helo Nala,
>
> (1)
>
>> To be clarified, use (http-method "/your_prefer_url" handler) is not
>> "script mode" only, it could appear in "application mode" (yes, MVC).
>> The controller is defined as GET in default.
>
> Ah, I see.
> So, by default, when you make a new "controller" in MVC mode, the
> controller code file has some default functions as templates, in the format
> "(<controller>-define <view> ...)", which is kind of equivalent to
> "(get "/<controller>/<view>" ...)".
> And the default template only works for GET requests, and nothing else.
> Right?
>

Yes, correct. Since we usually expect GET return a html page, so it's
naturally bind to a template view. Of course, this is just an common
assumption in my daily development. People may expect POST return a html
page too, depends on purpose. Anyway, I think (method url handler) way
is enough to cover all the cases.

> At least that's what I see testing so far.
>
> (2)
> So, I added in the controller file something like:
> "(post "/<controller>/<view>" ...)"
> And I confirmed now it doesn't give me errors when I send post requests,
> yay.
> However, now my
>   (get-from-qstr rc "some-varname")
> code doesn't work. (it's blank)
> Apparently, I can see the POST string in the body part. So I can
> actually see what I posted with:
>   (utf8->string (rc-body rc))
>
> # above being done after loading the module "rnrs bytevectors"
>
> But, I can't parse that string with get-from-qstr, it's just a string
> that looks like "name=value&".
>
> Is this expected behaviour? Or am I missing a function/doing something wrong?

For POST case, you should use :from-post
--------------------code--------------------
(post "/test" #:from-post 'qstr
(lambda (rc)
(:from-post rc 'get "some-varname")
...))
--------------------end---------------------

The reason to design :from-post is for the efficient purpose.
Artanis will not try to auto parse POST body as query-string for at least 2
reasons:
1. It may not be query-string, maybe json, or uploaded file
2. It may be long query-string, and could be deleyed to parse. This is
useful to avoid redundant parsing. However, each time you call
:from-post, it will parse the query-string again, the correct way to
fetch multiple values is:

-------------------code-------------------------
(:from-post rc 'get-vals "key1" "key2" "key3")
-------------------end--------------------------

Please make sure to use let-values or call-with-values to hold multiple
returned values.

Or you can just get the parsed query-string as an assoc-list, then get
the key as you want:

------------------------code-----------------------
(let ((ql (:from-post rc 'get)))
(assoc-ref ql "key1")
...)
------------------------end------------------------

I'll update the manual soon. You will see all the new things in next release.

Best regards.


>
> Cheers,
>
>
> Nala Ginrut <address@hidden> writes:
>
>> Hi Julio!
>>
>> Julio Claudio Matus Ramirez writes:
>>
>>> Hello.
>>>
>>> I'm using artanis in "application mode" (with art work, not running from
>>> a script-like single file, I think you call that "script mode").
>>>
>>> I see from the documentation that to accept data from POST requests, in
>>> script mode, you have to define a router with the method "post" for the path
>>> in question.
>>> I can't find in the documentation how to make that for an application made
>>> with "art".
>>>
>>> Can you tell me how to define a path (controller/view) in which I can
>>> use POST data?
>>
>> I think you mean that you can't define a POST controller by the
>> controller define helper function. For this purpose, you should use
>> (get "/your_prefer_url" handler)
>>
>> To be clarified, use (http-method "/your_prefer_url" handler) is not
>> "script mode" only, it could appear in "application mode" (yes, MVC).
>> The controller is defined as GET in default.
>>
>>>
>>> P.S.
>>> I see there's a ".route" file in the application dir that might be
>>> related, but I tried changing it and it gets overwritten. It'd be nice
>>> to have documented its purpose too. If you tell me I'll add it to the
>>> manuals.
>>
>> The route module is still suspended since I've always been blocking by
>> other higher priority tasks, file descriptors leaking, more efficient URL 
>> remapping, websocket, etc...
>>
>> For now, you may just simply ignore the .route file.
>> If you just want to handle RESTful APIs, the advanced route mapping is 
>> unecessary for
>> simple cases.
>>
>> --
>> GNU Powered it
>> GPL Protected it
>> GOD Blessed it
>> HFG - NalaGinrut
>> Fingerprint F53B 4C56 95B5 E4D5 6093 4324 8469 6772 846A 0058
>>


--
GNU Powered it
GPL Protected it
GOD Blessed it
HFG - NalaGinrut
Fingerprint F53B 4C56 95B5 E4D5 6093 4324 8469 6772 846A 0058

Attachment: signature.asc
Description: PGP signature


reply via email to

[Prev in Thread] Current Thread [Next in Thread]