I am currently working on a Django application that exposes an API with the following URL configuration:
url('^links/', linkhandler),
The link handler is a Django piston resource with a POST (Create function) outlined below:
def create(self, request):
try:
link_obj = Link.objects.get(link = request.POST['link'])
print link_obj
link_obj.rec_count = link_obj.rec_count + request.POST.get('rec_count', 1)
link_obj.save()
return link_obj
except:
try:
query_obj = Query.objects.get(query_word = request.POST['query'])
print query_obj
except:
query_obj = Query(query_word = request.POST['query'])
query_obj.save()
link_obj = Link(link = request.POST['link'], rec_count = request.POST.get('rec_count', 1), query = query_obj)
link_obj.save()
return link_obj
Everything seems to be functioning properly and I can successfully make POST requests using CURL. An example of a CURL request that works is shown below:
curl -d "query=hp&link=http://www.shopping.hp.com/&rec_count=1" http://localhost:8000/api/links/
However, when attempting this from a Greasemonkey script, it consistently returns a 400 error.
Below is the relevant Greasemonkey script:
GM_xmlhttpRequest({
method:"POST",
url:"http://localhost:8000/api/links/",
headers:{
"User-Agent":"Mozilla/5.0",
"Accept":"text/json",
"Content-Type" : "application/x-www-form-urlencoded"
},
data: encodeURI("query="+GM_getValue('q', '')+"&link="+this.previousSibling.href+"&rec_count=1"),
onerror: function errorhand(){
alert("An error occurred!");
}
});
What could potentially be causing this issue?