title: Invalid unicode while sending a POST request to django with libcurl and jsoncpp
body: <p>I've created a REST endpoint in Django using the rest-framework module; the simple code goes as below:</p>
  
  <p>models.py</p>
  
  <pre><code>class Data(models.Model):
      name = models.CharField(max_length=256)
      description = models.TextField()
  
      def __str__(self):
          return self.name
  </code></pre>
  
  <p>serializers.py</p>
  
  <pre><code>class DataSerializer(serializers.ModelSerializer):
      class Meta:
          model = Data
          fields = ('name', 'description')
  </code></pre>
  
  <p>views.py</p>
  
  <pre><code>def data_list(request):
      """
      List all data.
      """
      if request.method == 'GET':
          categories = Data.objects.all()
          serializer = DataSerializer(categories, many=True)
          return JSONResponse(serializer.data)
  
      elif request.method == 'POST':
          data = JSONParser().parse(request)
          serializer = DataSerializer(data=data)
          if serializer.is_valid():
              serializer.save()
              return JSONResponse(serializer.data, status=201)
          return JSONResponse(serializer.errors, status=400)
  </code></pre>
  
  <p>I've tried sending POST requests using the RESTClient plugin for Firefox, and can validate that it works as-is.</p>
  
  <p>However, my use-case is that I'd like to write to the database using libcurl in a C++ application.</p>
  
  <p>If I use jsoncpp to create a JSON object, and then use libcurl to make a POST request, as below:</p>
  
  <pre><code>void main() {
      Json::Value submitted_data;
      submitted_data["name"] = "data id";
      submitted_data["description"] = "data description";
      Json::StyledWriter writer;
  
      CURL *curl;
      CURLcode res;
  
      curl_global_init(CURL_GLOBAL_ALL);
  
      curl = curl_easy_init();
      if (curl) {
          curl_easy_setopt(curl, CURLOPT_URL, "http://127.0.0.1:8000/data/");
          struct curl_slist *headers = NULL;
          headers = curl_slist_append(headers, "Content-Type: application/json; charset=UTF-8");
          curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
          curl_easy_setopt(curl, CURLOPT_POST, 1);
          curl_easy_setopt(curl, CURLOPT_POSTFIELDS, writer.write(submitted_data).c_str());
  
          res = curl_easy_perform(curl);
          if (res != CURLE_OK)
              fprintf(stderr, "curl_easy_perform() failed: %s\n",
              curl_easy_strerror(res));
  
          curl_easy_cleanup(curl);
      }
  }
  </code></pre>
  
  <p>I get an error from the django server:</p>
  
  <pre><code>  File "C:\Python27\lib\site-packages\rest_framework\parsers.py", line 67, in parse
      raise ParseError('JSON parse error - %s' % six.text_type(exc))
  ParseError: JSON parse error - 'utf8' codec can't decode byte 0xdd in position 0: invalid continuation byte
  </code></pre>
  
  <p>And the post request isn't successful.  My understanding is that:</p>
  
  <ul>
  <li>django-rest-framework expects a utf-8 encoded json string,</li>
  <li>jsoncpp encodes strings in utf-8, and</li>
  <li>libcurl is agnostic about encoding and deals with data at the byte level.</li>
  </ul>
  
  <p>so I'm a bit surprised by this and not sure how to begin troubleshooting.  Can someone help me figure out how to have my C++ application and django application work together?</p>
  
  <p>thanks!</p>