Greasemonkey POST 请求总是返回 400 错误

发布于 2024-10-23 00:13:08 字数 1730 浏览 1 评论 0原文

我有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

旧城空念 2024-10-30 00:13:08

抱歉产生噪音,似乎是 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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文