tamizhgeek
tamizhgeek

Reputation: 1401

Greasemonkey POST request always returns 400 error

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?

Upvotes: 0

Views: 787

Answers (1)

tamizhgeek
tamizhgeek

Reputation: 1401

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

Upvotes: 2

Related Questions