Greasemonkey POST 请求总是返回 400 错误
我有一个 Django 应用程序,它公开一个 API,其 url conf 如下所示,
url('^links/', linkhandler),
链接处理程序是一个 django 活塞资源,其 POST(Create function) 我下面给出了,
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
以上所有内容都很好,当我通过 CURL 执行 POST 请求时,它工作得很好。 例如,下面是我的 CURL 请求,它有效,
curl -d "query=hp&link=http://www.shopping.hp.com/&rec_count=1" http://localhost:8000/api/links/
但是当我从 Greasemonkey 脚本中尝试此操作时,它总是返回 400 错误:(
下面是相关的 Greasemonkey 脚本
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("error occurred!");
}
});
可能是什么问题?
I have a django application which is exposing a API whose url conf look like this,
url('^links/', linkhandler),
The link handler is a django piston resource whose POST(Create function) I have given 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
All the above is fine and When I do POST request throught CURL, it works perfectly fine.
For example the below is my CURL request which works,
curl -d "query=hp&link=http://www.shopping.hp.com/&rec_count=1" http://localhost:8000/api/links/
But When I try this from a greasemonkey script, it always gives back a 400 error :(
Below is the related 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("error occurred!");
}
});
What could be the issue?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
抱歉产生噪音,似乎是 django 活塞问题。
这是详细信息。
https://bitbucket.org/ jespern/django-piston/issue/87/split-charset-encoding-form-content-type
Sorry for the noise, seems like it's a django-piston issue.
Here is the details.
https://bitbucket.org/jespern/django-piston/issue/87/split-charset-encoding-form-content-type