Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Post not sending data properly #418

Closed
Adman opened this issue Aug 18, 2016 · 3 comments
Closed

Post not sending data properly #418

Adman opened this issue Aug 18, 2016 · 3 comments

Comments

@Adman
Copy link

Adman commented Aug 18, 2016

Hello,
I have a django application where I want to make POST by clicking on <button> which is not in <form>
The problem is that data sent to view function are not being sent properly.

Here is axios code:

 axios({
        method: 'post',
        url: '/url/to/view'
        data: {
            obj_id: objectid,
            content_type: contenttype,
            like: !is_liked
        },
        xsrfCookieName: 'csrftoken',
        xsrfHeaderName: 'X-CSRFToken',
        headers: {'X-Requested-With': 'XMLHttpRequest',
                  'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'},
    }).then(function (response) { 
        console.log(response);
    });

and here view code:

def my_view(request):
    print request.POST
    return HttpResponse('XX')

I cannot access request.POST data because the key is entire dict and value empty list:
<QueryDict: {u'{"obj_id":"182","content_type":"video","like":true}': [u'']}>

@nickuraltsev
Copy link
Member

The problem is that objects are serialized to JSON by default while you need to send the data in the `application/x-www-form-urlencoded' format. The easiest solution is to use a library like qs:

var qs = require('qs');
axios.post('/foo', qs.stringify({ 'bar': 123 });

See this comment for details.

Hope this helps!

@bubuzzz
Copy link

bubuzzz commented Nov 20, 2016

Another way is putting the values into the FormData object and post

@vanpav
Copy link

vanpav commented Feb 14, 2017

I wrote small wrapper aroud axios which works fine with default Django >1.8 settings. Hope this helps somebody!

@axios axios locked and limited conversation to collaborators May 21, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants