1

I have an AngularJS app, which is sending HTTP PUT data to flask; 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 HTTP PUT to the server...

Flask 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')

"formsubmit_add2" correctly GETs from /api/thing/hostform; however, for some reason request.form.get('hosts', "!! ERROR !!") always errors out with an HTTP 500 error as you can see below...

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 -

For anyone who is curious, request.json is None when I get the HTTP PUT...

Question

How can I correctly receive what AngularJS is sending to flask?


Wireshark dump of the HTTP PUT:

This is a wireshark dump of the HTTP PUT from AngularJS...

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"}]}

AngularJS $http PUT:

        $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
            );
2
  • Why are you sending as application/x-www-form-urlencoded instead of application/json? Commented Apr 11, 2014 at 18:56
  • @astex, using application/json fixed the problem... I have successfully communicated otherwise with application/x-www-form-urlencoded in another flask + angular project but something clearly is different here. Please post that as an answer Commented Apr 11, 2014 at 19:04

2 Answers 2

3

You are using the wrong mimetype for JSON. Rather than application/x-www-form-urlencoded, it should be application/json.

Sign up to request clarification or add additional context in comments.

Comments

1

From the Flask Request class documentation:

json

If the mimetype is application/json this will contain the parsed JSON data. Otherwise this will be None.

The get_json() method should be used instead.

I think your problem appears because request.form doesn't contain hosts key (contains something else), because your mimetype is not json (application/json).

For a more precise answer please write what comes in request.form?

4 Comments

Regarding what comes in request.form, the hosts key is there AFAICT... please look at the wireshark output near the bottom of my question. I will award the accept to astex as soon as he answers, but I should vote this up if you can describe why I couldn't see the host key in a application/x-www-form-urlencoded PUT
I mean that there is not the format inside request.form what do you expect as properly delivered mimetype; if you print it, you'll see it.
I'm not sure what you mean when you say "if you print it, you'll see it" print request.form is an ImmutableMultiDict werkzeug object
Yes, but it's keys and values are not what you expect, because of wrong mimetype, so the error occurs. Am I right? :)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.