title: Receiving JSON from AngularJS in Flask
body: <p>I have an AngularJS app, which is sending <code>HTTP PUT</code> data to <a href="http://flask.pocoo.org/" rel="nofollow">flask</a>; I can see that the data arrives at the server correctly... however, for some reason, my flask method can't read it when Angular makes an <code>HTTP PUT</code> to the server...</p>
  
  <p><em>Flask code</em>:</p>
  
  <pre class="lang-python prettyprint-override"><code>@app.route('/api/thing/hostform', methods=['GET'])
  @login_required
  def get_blank_host_row():
      retval = [host_row(host_name="RANDOM_HOST")]
      return Response(dumps(retval), mimetype='application/json')
  
  @app.route('/api/thing/hostform', methods=['PUT'])
  @login_required
  def append_blank_host_row():
      retval = request.form.get('hosts', "!! ERROR !!")
      print "PUT RESULT", retval
      retval.append(host_row())
      return Response(dumps(retval), mimetype='application/json')
  </code></pre>
  
  <p>"formsubmit_add2" correctly GETs from <code>/api/thing/hostform</code>; however, for some reason <code>request.form.get('hosts', "!! ERROR !!")</code> always errors out with an HTTP 500 error as you can see below...</p>
  
  <pre><code>10.93.10.120 - - [11/Apr/2014 13:15:22] "GET /formsubmit_add2 HTTP/1.1" 200 -
  10.93.10.120 - - [11/Apr/2014 13:15:22] "GET /api/thing/hostform HTTP/1.1" 200 -
  PUT RESULT !! ERROR !!
  10.93.10.120 - - [11/Apr/2014 13:15:25] "PUT /api/thing/hostform HTTP/1.1" 500 -
  </code></pre>
  
  <p>For anyone who is curious, <code>request.json</code> is <code>None</code> when I get the <code>HTTP PUT</code>...</p>
  
  <h3>Question</h3>
  
  <p>How can I correctly receive what AngularJS is sending to <a href="http://flask.pocoo.org/" rel="nofollow">flask</a>?</p>
  
  <hr>
  
  <p><em>Wireshark dump of the HTTP PUT</em>:</p>
  
  <p>This is a wireshark dump of the HTTP PUT from AngularJS...</p>
  
  <pre><code>Hypertext Transfer Protocol
      PUT /api/thing/hostform HTTP/1.1\r\n
          [Expert Info (Chat/Sequence): PUT /api/thing/hostform HTTP/1.1\r\n]
              [Message: PUT /api/thing/hostform HTTP/1.1\r\n]
              [Severity level: Chat]
              [Group: Sequence]
          Request Method: PUT
          Request URI: /api/thing/hostform
          Request Version: HTTP/1.1
      Host: tsunami:5000\r\n
      User-Agent: Mozilla/5.0 (Windows NT 6.1; rv:28.0) Gecko/20100101 Firefox/28.0\r\n
      Accept: application/json, text/plain, */*\r\n
      Accept-Language: en-US,en;q=0.5\r\n
      Accept-Encoding: gzip, deflate\r\n
      Content-Type: application/x-www-form-urlencoded; charset=UTF-8\r\n
      Referer: http://server_name:5000/formsubmit_add2\r\n
      Content-Length: 164\r\n
          [Content length: 164]
      Connection: keep-alive\r\n
      \r\n
      [Full request URI: http://server_name:5000/api/thing/hostform]
  Line-based text data: application/x-www-form-urlencoded
      {"hosts":[{"real_ip_addr":"","switch_port":"","host_nic_role":"Console",
      "host_name":"RANDOM_HOST","host_nic_name":"","switch_name":"",
      "altn_ip_addr":"192.0.2.42"}]}
  </code></pre>
  
  <hr>
  
  <p><em>AngularJS $http PUT</em>:</p>
  
  <pre class="lang-javascript prettyprint-override"><code>        $scope.add_row = function (data) {
              // build an ajax request with $scope.data
              var send = $http({
                  method  : "PUT",
                  url     : "/api/thing/hostform",
                  headers : {"Content-Type":
                      "application/x-www-form-urlencoded; charset=UTF-8"},
                  data: {"hosts": data},
              });
              send.success(
                  // something irrelevant here
              );
  </code></pre>