title: Got Failed to decode JSON object when calling a POST request in flask python
body: <p>I have written a simple REST-ful web server in python with <code>flask</code> following steps in this <a href="http://blog.miguelgrinberg.com/post/designing-a-restful-api-with-python-and-flask" rel="noreferrer">tutorial</a>; but I've got a problem calling <code>POST</code> request. The code is:</p>
  
  <pre><code>@app.route('/todo/api/v1.0/tasks', methods=['POST'])
  def create_task():
      if not request.json or not 'title' in request.json:
          abort(400)
      task = {
          'id': tasks[-1]['id'] + 1,
          'title': request.json['title'],
          'description': request.json.get('description', ""),
          'done': False
      }
      tasks.append(task)
      return jsonify({'task': task}), 201
  </code></pre>
  
  <p>I send a <code>POST</code> request using <code>curl</code> as the example in the above mentioned page:</p>
  
  <pre><code>curl -i -H "Content-Type: application/json" -X POST -d '{"title":"Read a book"}' http://127.0.0.1:5000/todo/api/v1.0/tasks
  </code></pre>
  
  <p>But I get this error in response:</p>
  
  <pre><code>HTTP/1.0 400 BAD REQUEST
  Content-Type: text/html
  Content-Length: 187
  Server: Werkzeug/0.11.10 Python/2.7.9
  Date: Mon, 30 May 2016 09:05:52 GMT
  
  &lt;!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN"&gt;
  &lt;title&gt;400 Bad Request&lt;/title&gt;
  &lt;h1&gt;Bad Request&lt;/h1&gt;
  &lt;p&gt;Failed to decode JSON object: Expecting value: line 1 column 1 (char 0)&lt;/p&gt;
  </code></pre>
  
  <p>I've tried to debug and I found out in the <code>get_json</code> method, the passed argument has been translated to <code>'\\'{title:Read a book}\\''</code> as <code>data</code> and <code>request_charset</code> is <code>None</code>; but I have no idea for a solution. Any help?</p>
  
  <p><strong>EDIT 1:</strong></p>
  
  <p>I have tried @domoarrigato's answer and implemented the <code>create_task</code> method as the following:</p>
  
  <pre><code>@app.route('/todo/api/v1.0/tasks', methods=['POST'])
  def create_task():
      try:
          blob = request.get_json(force=True)
      except:
          abort(400)
      if not 'title' in blob:
          abort(400)
      task = {
          'id': tasks[-1]['id'] + 1,
          'title': blob['title'],
          'description': blob.get('description', ""),
          'done': False
      }
      tasks.append(task)
      return jsonify({'task': task}), 201
  </code></pre>
  
  <p>But this time I got the following error after calling <code>POST</code> via <code>curl</code>:</p>
  
  <pre><code>HTTP/1.0 400 BAD REQUEST
  Content-Type: text/html
  Content-Length: 192
  Server: Werkzeug/0.11.10 Python/2.7.9
  Date: Mon, 30 May 2016 10:56:47 GMT
  
  &lt;!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN"&gt;
  &lt;title&gt;400 Bad Request&lt;/title&gt;
  &lt;h1&gt;Bad Request&lt;/h1&gt;
  &lt;p&gt;The browser (or proxy) sent a request that this server could not understand.&lt;/p&gt;
  </code></pre>
  
  <p><strong>EDIT 2:</strong></p>
  
  <p>To clarify, I should mention that I'm working on a 64-bit version of Microsoft Windows 7 with Python version 2.7 and the latest version of Flask.</p>