3

I'm trying to make a put request to the local server using the put request using curl:

curl -X PUT -H "Content-Type: application/json" -d '{"connid":"12"}' "127.0.0.1:8000/api/kill"

I receive the same response:

'WSGIRequest' object has no attribute 'PUT'

for the following code:

def kill(req):
  conid = req.PUT['connid']
  statusres = {}

  if conid in state:
    error[conid] = 'true'
    statusres['status'] = 'ok'

  else:
    statusres['status'] = 'invalid connection Id : '+ conid
  return JsonResponse(statusres)

I also used @csrf_exempt before the function.

trincot
  • 317,000
  • 35
  • 244
  • 286
Anmol Jindal
  • 31
  • 1
  • 3
  • What is `req`? Is this a Django view? The argument is usually called `request`. Either way, what makes you think it has an argument called `PUT`? – Daniel Roseman Apr 02 '17 at 16:48
  • req is the request object . yes this is a django view.. here [link](http://stackoverflow.com/questions/4994789/django-where-are-the-params-stored-on-a-put-delete-request) @DanielRoseman – Anmol Jindal Apr 02 '17 at 17:03
  • Yes. Did you read that link? It explicitly says you can't do this. But you don't want to anyway, given that you are sending JSON. – Daniel Roseman Apr 02 '17 at 17:08
  • i miss read that. do i need to specify put as method to the request explicitly @DanielRoseman – Anmol Jindal Apr 02 '17 at 17:24

1 Answers1

8

You have misunderstood several things here.

When you send form-encoded data, whether it's POST or PUT, in Django you always find the parameters in request.POST. So you would find your data in request.POST['conid'].

However, you are not sending form-encoded data; you are sending JSON. You need to access the request body, and pass it to the json.loads function to decode:

def kill(request):
  data = json.loads(request.body)
  conid = data['connid']
Daniel Roseman
  • 588,541
  • 66
  • 880
  • 895