I am having an issue when trying to POST data. My server keeps rejecting my data (500 server error).
Is it the way I have it formatted? I noticed in my console in Response Headers it says "Content-Type:text/html". Should that say JSON?
In python, this works fine:
requests.post('http://test.net/item/291/', {'uid':21, 'click':1, 'like':1, 'image':0, 'scroll':1, 'clickbuy':0})
I setup a middleware file which includes:
class CorsMiddleware(object):
def process_response(self, request, response):
response['Access-Control-Allow-Origin'] = '*'
response['Access-Control-Allow-Methods'] = 'POST, GET, OPTIONS, PUT'
return response
My AngularJS app is setup like this:
.factory('cardsApi', ['$http', function ($http) {
var like = JSON.stringify({uid:21, click:1, like:1, image:0, scroll:1, clickbuy:0}));
var postLikes = function (product_id) {
return $http.post('http://test.com/api/item/' + product_id, like);
}
return {
postLikes: postRecordLikes
};
}])
.config(function ($httpProvider) {
$httpProvider.defaults.headers.common = {};
$httpProvider.defaults.headers.post = {};
$httpProvider.defaults.headers.put = {};
$httpProvider.defaults.headers.patch = {};
})
The error (in browser):
The answer is in the exception value at the bottom of the traceback you pasted.
Exception Value: You called this URL via POST, but the URL doesn't end in a slash and you have APPEND_SLASH set. Django can't redirect to the slash URL while maintaining POST data. Change your form to point to stashdapp-t51va1o0.cloudapp.net/api/item/37/ (note the trailing slash), or set APPEND_SLASH=False in your Django settings.
For POST requests, make sure the URL matches the pattern defined in your urls.py
(or in whatever 3rd party Django app you're using that may be defining the URL patterns for you). In this case, your URL pattern includes a trailing slash, so you must include one with the URL for your POST request.
By default, Django automatically redirects URLs without trailing slashes to URLs with trailing slashes, but for POST requests you'll lose the submitted data and this exception will be raised.
p.s. Unless you've tried it already and there's some reason it doesn't work well for you, I'd definitely recommend using the django-cors-headers app as opposed to rolling your own middleware to deal with CORS.