Mojolicious Angular POST JSON - angularjs

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 ?

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.

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.

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.
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.
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.)

I don't think you're supposed to JSON-stringify your object.
Try replacing your first angular line with this:
var datainput = {test: "orcl"};

Related

Getting json data from url

How do I get a JSON response from URL?
In my case, the URL is https://api.mathjs.org/v4/?expr=2*2 JSON response and then whenever someone types !test it sends the json/data from the URL.
Here is some sample code using node-fetch, I cant guarantee it will work well
const fetch = require('node-fetch')
(async () => {
const response = await fetch(`https://api.mathjs.org/v4/?expr=${encodeURIComponent(2*2)}`).then(r => r.text())
console.log(response)
})()
this logs 4.
this is a sample code as i said,
you would have to modify it to your needs, i used a iife as fetch method returns a promise
you can do:
<message>.channel.send(`the result is ${response}`)
as a example adapted from the above given code
ps: rather than using a api for math you could use mathjs package (which your using, just its the api version)
Edited: forgot of encodeURIComponent in fetch request url, or you will get a Only absolute URLs are supported error
You don't need JSON just to get the value of the query parameter from the URL. If you just need to get the number you can try it with URLSearchParams:
console.log(window.location.search); //output: '?expr=2*2'
var params = new URLSearchParams('?expr=2*2');
console.log(params.has('expr')); //output: 'true';
console.log(params.get('expr')); //output: '2*2';
you get the value from params.get('expr'));

How to create a Perl API to pass data back to an angularjs $http request?

I'm using angular 1.5.7 and perl 5.16.2. I don't have the ability to use any external libraries or switch tools - I'm stuck just using what's included with these two.
The angular controller is making calls to a perl file. I'm able to make POST requests successfully, and my GET requests are returning status 200 but aren't showing the data that I am expecting to see. Right now I'm just working with a simple example to try and narrow down where things are going wrong, and I think I just don't know the format to return values from the perl file.
My controller GET method looks like so:
$http({
method : "GET",
url : "filename.pl",
params:{}
}).then(function successCallback(response) {
console.log(response.records);
}, function errorCallback(response) {
console.log(response);
});
and the perl code being called in filename.pl is:
my $string = q{{"records":{"2":{"City":"México D.F.","Country":"Mexico","Name":"Ana Trujillo Emparedados y helados"},"1":{"Name":"Alfreds Futterkiste","Country":"Germany","City":"Berlin"}}}};
return $string;
The string is just a dummy variable to see if passing things back works - it won't be the final data. I have verified it is valid JSON as well. Here's what the response from the server looks like:
{"data":"","status":200,"config":{"method":"GET","transformRequest":[null],"transformResponse":[null],"url":"glwizardutils.esp","params":{"FUNCTION":"initcheckboxes","CONTEXTID":"432"},"headers":{"Accept":"application/json, text/plain, */*"}},"statusText":"OK"}
The data field is totally blank, and the response field doesn't work at all. I can verify it's hitting the perl file - like I said, POST requests work fine and I included some printouts that verify it's in the correct method, but it's just not returning anything. What should I be doing here instead to get this to work?
If filename.pl is invoked with HTTP, then it should be writing an HTTP response to standard output. At a minimum:
print "HTTP/1.1 200 OK\n";
print "Content-type: application/json\n";
print "\n";
my $string = q{{"records":{"2":{"City":"México ..."}}}};
print $string;
Several frameworks and modules exist in Perl and virtually every other language to handle the repetitive aspects of writing a proper response, which you will want to look into as your Perl server-side script gets more demanding.

Nodejs not sending response to Angular?

I have a doozy that I can't figure out whats going on.
In Mongo, I do a document.find(), which returns an array of object/s - All good.
I'm then trying to send on two objects back to the Angular Controller, the document object/s and the length of the array of objects.
Code:
function loadConnections(req, res) {
getConnections(req.user)
.then(function(results){
console.log('here');
console.log(results);
console.log(results.length);
var returnObject = {}
returnObject.count = results.length;
//returnObject.results = results[0]; // PROBLEM LINE
res.status(200).send(returnObject);
});
}
Problem I'm facing. In this scenario, it returns an array with 1 object. The array looks like:
[{id: XXX, test: YYY, test1: ZZZ}]
These have been what I've tried and tested:
returnObject.results = results[0].id; // works
returnObject.results = results[0].test; // works
returnObject.results = results[0].test1; //works
returnObject.results = results[0]; // doesn't work
returnObject.results = results; //doesn't work
But if I try to pass the entire object or the entire array, it hits an issue and doesn't send the response to the controller.
Any thoughts?
If you are using express, try to do a
res.send(200, returnObject);
or
res.json(200, returnObject);
that should do the trick!
Personally I would just calculate the length of the array after it arrives in my Angular client, rather than on the server. However, you should be able to accomplish what you're trying to do by doing this:
var returnObject = {
'count': results.length,
'results': results
};
res.json(returnObject);
Here's the doc: http://expressjs.com/en/api.html#res.json
Also just for general troubleshooting, the first thing you want to do is figure out where the inconsistency is starting. Does the data look right on the server before you send the response? Does the data look right on the client when your API handler receives the response? (I'm assuming $resource, or maybe you're using $http). So add a line like this just before you send the response from the server, and then add a similar line just after you receive the response in the client:
console.log(JSON.stringify(returnObject, null, 2));
Turns out it was my lodash library. I was using _.pluck and somewhere along the way, it's stopped working. Had to update it to _.map.

Restangular getList() does not return array of list items

I try to use Restangular to handle calls to my restful API.
Here is my code:
var baseStories = Restangular.all('stories/all');
baseStories.getList().then(function (stories) {
console.log(stories);
})
The console.log shows the full restangularized array instead of an array of stories as I'd expect.
I use the RestangularProvider.addResponseInterceptor from the docs to unwrap the response data.
Has anyone an idea what I'm missing?
Edit:
Below is a screenshot of the console.log output from the code above. I see two stories (which is correct) and a bunch of Restangular methods. Is it possible to only get the stories?
Actually addResponseInterceptor must return restangularized element. It is written in the documentation:
https://github.com/mgonto/restangular#addresponseinterceptor
In order to get clean response you have to call plain() method on the response element:
var baseStories = Restangular.all('stories/all');
baseStories.getList().then(function (response) {
$scope.stories = response.plain();
})

SYmfony2 request can't get the post variable

I'm trying to get the variable of a POST request in a symfony 2.4 controller. I usually have no issues on that but for this one...
this is how I call the controller:
var deferred = $q.defer();
$http({method:'POST',data:{dimensionpassed:dimension},url:Routing.generate('_API_getTree')}).success(function(result){
deferred.resolve(result);
});
return deferred.promise;
Now when I look in the console I can see the post request:
JSON
dimensionpassed
"ChartOfAccounts"
Source
{"dimensionpassed":"ChartOfAccounts"}
And here is the controller:
public function _API_getTreeAction(Request $request)
{
$test = $request->getContent();
var_dump($test);
$test = $request->request->get('dimensionpassed');
var_dump($test);
}
The first var dump gives me
"{"dimensionpassed":"ChartOfAccounts"}"
so there is something !
but the second is only "NULL"
what am I doing wrong ?
Apparently, Angularjs $http post variable don't go to the $_POST variable, hence it's impossible to have them in the $request->request->all().
However they appear "raw" as you can have them with this:$request->getContent();
Simply using $myvar = json_decode($request->getContent()) does the trick
I don't know angularjs at all, but that looks like JSON in the body, not request body parameters.
Try:
var_dump($request->request->all());
If you're sending post parameters you should see something like:
array 'dimensionpassed' => string 'ChartOfAccounts'
And the request body should look something like:
'dimensionpassed=ChartOfAccounts'
So you probably just need to get your POST from Javascript working properly (can't help there sorry).

Resources