i am new to Perl and Mojo and i've got one problem by receiving POST-Data from Angular:
My AngularCode is
var datainput = JSON.stringify({"test":"orcl"});
$http.post('http://localhost/perltest/perltest.pl/post', datainput)
.success(function(data, status, headers, config) {
console.log("post geschickt");
console.log(headers());
console.log(data);
console.log("data back: " + JSON.stringify(data));
alert(JSON.stringify(data));
})
My Mojo-Sub looks like:
post '/post' => sub {
my $self = shift;
my $json = $self->req->json;
print header(-type => "text/html");
print Dumper($json->{test});
};
app->start;
The Result i get is: $VAR1 = undef; Content-Length: 0 Status: 404 Not Found Date: Fri, 20 Jan 2017 09:49:57 GMT
What is wrong? It seems to me that $json = $self->req->json is not getting the JSON-String from POST ?
The docs for the
jsonmethod say thatundefis returned if decoding didn't work or if the request was empty. You should first look at the request body.This will output the raw request body to your application console or log. If you run
morbo app.pl, that's your console window. Look at what you see. Is the content there? Is the content-type correct?Then take it from there.
You can't just
printin the middle of your route handler. You need to use the application object torenderyour content.This way, Mojolicious takes care of everything for you. There is also no need to set the content type explicitly. It's going to use something sensible automatically.
However, you're getting a 404 back. That might be because of the
print, but I'm not sure.