Last active
August 29, 2015 14:12
-
-
Save eviltik/30f92cf68aa8f25ea14d to your computer and use it in GitHub Desktop.
mojolicious: access to a json attribute from a json post request
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package myProject::Webservice; | |
use Mojo::Base 'Mojolicious::Controller'; | |
use constant true => 1; | |
use constant false => 0; | |
sub userLogin { | |
my $self = shift; | |
my $data = $self->req->json; | |
#REQUEST: | |
#-------------------------------------------- | |
#Accept:application/json, text/javascript, */*; q=0.01 | |
#Accept-Encoding:gzip, deflate | |
#Accept-Language:en-US,en;q=0.8,fr-FR;q=0.6,fr;q=0.4 | |
#Connection:keep-alive | |
#Content-Length:37 | |
#Content-Type:application/json | |
#Cookie: SESSION=eyJleHBpcmVzIjoxNDE5NjY4Mjg3LCJjc3JmX3Rva2VuIjoiZDM1MDZjNmU1MmFjMDFlNTc2YjU2ZGRmMjk3ODExMmE4ZTBhNTVjZCJ9--60a01b0bb1ec552d8d6248716a2f6034481a3abc | |
#Host:192.168.56.101:3000 | |
#Origin:http://192.168.56.101:3000 | |
#Referer:http://192.168.56.101:3000/login.html | |
#User-Agent:Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.95 Safari/537.36 | |
#X-Requested-With:XMLHttpRequest | |
#{"username":"test","password":"test"} | |
#-------------------------------------------- | |
# log: [error] Bareword "username" not allowed while "strict subs" in use | |
$self->app->log->info($data.username); | |
# log: [error] Bareword "username" not allowed while "strict subs" in use | |
$self->app->log->info($data => username); | |
# log: HASH(0x21b78c8), except json string like when render below | |
$self->app->log->info($data); | |
# output nice json {"password":"test","username":"test"} | |
$self->render( json => $data ); | |
return true; | |
} | |
1; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
thanks to irc #mojo
Good answer is :