I'm trying to post events into a CherryPy small app which responds to GET/POST but when trying it with AngularJS it simply not posting a thing.
Now, I don't know whether this has to do with AngularJS or with CherryPy (CP), cross domain is enabled in CP but it simply can't get the POST method. However this works with CURL
POST in CP is defined as:
def POST(self, date, description):
# read items to catch any server side update
events = {
"date": int(date.encode('ascii','ignore')),
"description": description.encode('ascii','ignore'),
"status":"Active"
}
# storing changes
save_events(events)
return ('Event created\n')
Headers are enabled by:
def enableCrossDomain():
cherrypy.response.headers["Access-Control-Allow-Origin"] = "*"
cherrypy.response.headers["Access-Control-Allow-Methods"] = "GET, POST"
cherrypy.response.headers["Access-Control-Allow-Headers"] = "Cache-Control, X-Proxy-Authorization, X-Requested-With"
def OPTIONS(self):
enableCrossDomain()
return
And a CURL call looks like this:
curl -d $EPOCH_DATE -d $EVENT -X POST 'myurl.com:1234/api/events/'
Trying this with AngularJS:
var message = "'" + $scope.event_description + "' '" + $scope.datetime_start + "'";
console.log(message);
$scope.post_url = "myurl.com:1234/api/events";
$http.post($scope.post_url,message).success();
Already tried posting "message" as a JSON and specifying it in the headers but just can't get it to work. Any ideas what am I missing or what else should I try here?
It seems that the answer was quite simpler:
The URL was not properly encoded so the browser was going bonkers; changed this:
$scope.post_url = "myurl.com:1234/api/events";
For
$scope.post_url = "http://myurl.com:1234/api/events";
Then just the message was being wrongly parsed, that I noticed by the following message:
HTTPError: (404, 'Missing parameters: message')
For the sake of simplicity changed the parameters to just one message and split it internally in python, then my whole POST call changed to this:
$scope.post_url = "http://myurl.com:1234/api/events";
var message = 'message=' + $scope.datetime_start + ',' + $scope.event_description;
$http({
method: 'POST',
url: $scope.post_url,
data: message,
headers: {'Content-Type': 'application/x-www-form-urlencoded'}
});
And important as well, on the python end I was not appending the headers to the POST message, changed the following:
def POST(self, date, description):
# read items to catch any server side update
events = {
"date": int(date.encode('ascii','ignore')),
"description": description.encode('ascii','ignore'),
"status":"Active"
}
# storing changes
save_events(events)
return ('Event created\n')
For this:
def POST(self, message):
# read items to catch any server side update
events = {
"date": int(message.split(',')[0].encode('ascii','ignore')),
"description": message.split(',')[1].encode('ascii','ignore'),
"status":"Active"
}
# storing changes
save_events(events)
enableCrossDomain() # HEADERS HERE OR AJAX GETS ANGRY
return ('Event created\n')
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With