get Json data from request with Django
Question: I'm trying to develop a very simple script in Django, I'd collect a Json data
from the request and then store all data in the database.

I developed one python script that I'm using to send the Json data to the
Django view, but I'm doing something wrong and I can't understand what,
because every time that I run it,I've got "Malformed data!".

Can someone helps me? what am I doing wrong?

Sender.py

    
    
    import json
    import urllib2
    
    data = {
             'ids': ["milan", "rome","florence"]
    }
    
    req = urllib2.Request('http://127.0.0.1:8000/value/')
    req.add_header('Content-Type', 'application/json')
    
    response = urllib2.urlopen(req, json.dumps(data))
    

Django view.py

    
    
    from django.shortcuts import render
    
    # Create your views here.
    from django.http import HttpResponse
    import json
    from models import *
    
    from django.http import StreamingHttpResponse
    from django.views.decorators.csrf import csrf_exempt
    
    @csrf_exempt
    def value(request):
    
      try:
          data = json.loads(request.body)
          label = data['label']
          url = data ['url']
          print label, url
      except:
          return HttpResponse("Malformed data!")
      return HttpResponse("Got json data")
    


Answer: Your dictionary "data" in sender.py contains only one value with key "ids" but
in view.py you are trying to access keys "label" and "url" in this parsed
dictionary.

