Mojolicious Angular POST JSON

914 Views Asked by At

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 ?

4

There are 4 best solutions below

0
On
  1. The 404 Not Found indicates the resource is not found. Please double check if your application is available under http://localhost/perltest/perltest.pl/post.

  2. You should not use print(), because it's not reliable (sometimes it works and sometimes not). If you want to log text into your console, please use $self->app->log->debug(). Mojolicious also has $self->dumper, you do not need to include the external module Data::Dumper.

  3. Check the data which is actually send. You can use a service like http://requestb.in/. If you recieve correct JSON; I would strongly expect the URL is not correct (see point 1.)

2
On

The docs for the json method say that undef is returned if decoding didn't work or if the request was empty. You should first look at the request body.

warn Dumper $self->req->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 print in the middle of your route handler. You need to use the application object to render your content.

post '/post' => sub {
    my $self = shift;
    my $json = $self->req->json;

    $self->render( text => $json->{test} );
};

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.

0
On

I don't think you're supposed to JSON-stringify your object.

Try replacing your first angular line with this:

var datainput = {test: "orcl"};
0
On

Angular passes posts in via the body of the request, so this is how I handle those.

post '/post' => sub {
    my $self = shift;
    my $json = $self->req->body;
    #use Mojo::JSON qw(decode_json encode_json) at top of app
    my $perl_hash = decode_json($json)
    #Do something with the hash, like pass to a helper or model
    $self->render(json => $return_from_model_or_helper);
};

Jquery posts will use the params and not the body.