I'm trying to make a REST API in Django by outputting Json. I'm having problems if I make a POST request using curl in terminal. The error I get is:
You called this URL via POST, but the URL doesn't end in a slash and you have APPEND_SLASH set. Django can't redirect to the slash URL while maintaining POST data. Change your form to point to 127.0.0.1:8000/add/ (note the trailing slash), or set APPEND_SLASH=False in your Django settings.
My url.py is:
from django.conf.urls.defaults import patterns, include, url
import search
# Uncomment the next two lines to enable the admin:
# from django.contrib import admin
# admin.autodiscover()
urlpatterns = patterns('',
url(r'^query/$', 'search.views.query'),
url(r'^add/$','search.views.add'),
)
And my views are:
# Create your views here.
from django.http import HttpResponse
from django.template import Context,loader
import memcache
import json
def query(request):
data=['a','b']
mc=memcache.Client(['127.0.0.1:11221'],debug=0)
mc.set("d",data);
val=mc.get("d")
return HttpResponse("MEMCACHE: %s<br/>ORIGINAL: %s" % (json.dumps(val),json.dumps(data)) )
def add(request):
#s=""
#for data in request.POST:
# s="%s,%s" % (s,data)
s=request.POST['b']
return HttpResponse("%s" % s)
I know its not giving Json but I'm having the problem mentioned above when I make POST request in terminal
curl http://127.0.0.1:8000/add/ -d b=2 >> output.html
I'm new to django though.
First, make sure that you send the request to http://127.0.0.1/add/
not http://127.0.0.1/add
.
Secondly, you may also want to exempt the view from csrf processing by adding the @csrf_exempt
decorator - since you aren't sending the appropriate token from cURL.