I am sending a POST request to the server application using superagent.js. The request is sent to a django
view. But I am getting a GET request on the view side.
Here is my code where I am trying to send a request with superagent
:
request.post('/nameofview')
.set('Content-type', 'application/x-www-form-urlencoded')
.set('X-CSRFToken', csrf)
.send({name: 'name'})
.end(callback)
These are logs from the django
development server:
[30/Mar/2016 12:27:28] "POST /nameofview HTTP/1.1" 302 0
[30/Mar/2016 12:27:28] "GET /ru/nameofview HTTP/1.1" 404(<--Raising not found from within the view) 1696
So, django
project url dispatcher is receiving a post request but then redirecting it to the application view as a get request. I don't have any problems with url matching. Execution enters view code, but it is a get request not a post and that's why I can't get the post data I have sent.
This is my view code:
def nameofview(request):
if 'name' in request.POST:
# make some staff
return JsonResponse({'result': result})
else:
raise Http404()
From this post, I figured out that I have to attach a csrftoken to my post request, and of course I did this as it is described here without any success.
So, my question is, why it is doing this and how can I make it not touch my post request?
P.S. It works with get request without any problem.
Mounir seems to be on the right track. However the trailing /
does not seem to be the problem. Instead, Django is adding a /ru
to the front of the requested URL. Could ru
possibly be the descriptor for a language that you encode in your url? If so, try to call request.post('/ru/nameofview/')
.
On the other hand, superagent seems to call the redirects with a GET
request instead of a POST
so the allow_redirects=True
function that Mounir proposed could also do the trick, though I am not familiar with that library.