librelist archives

« back to archive

difference between request.data and request.values

difference between request.data and request.values

From:
Cornelius Kölbel
Date:
2014-12-29 @ 17:49
Hi,

I have a problem with my flask REST API and an AngularJS web application.

Can someone please point me to the difference between flasks

 request.data and request.values

in a POST request?

One of both will probably contain the form data.
request.data containes a str while the request.values contains a dict.

I have an API, which at the moment is supposed to retrieve a dict in
request.values.
It works fine with my tests like this:

with self.app.test_request_context('/resolver/r1',
                                   data={u"resolver": u"r1",
                                         u"type": u"passwdresolver",
                                         u"fileName": PWFILE},
                                   method='POST',
                                   headers={"Authorization": self.at}):
    res = self.app.full_dispatch_request()


In this case the request.values contains the dictionary defined in data.

But when I try to pass a JSON object from a angularJS web app,
request.values is empty and the dictionary is contained as a serialized
string in request.data. The request looked like this:

 1. Request Headersview source
     1.
        Accept:
        application/json, text/plain, */*
     2.
        Accept-Encoding:
        gzip, deflate
     3.
        Accept-Language:
        en-US,en;q=0.8,de-DE;q=0.6,de;q=0.4
     4.
        Authorization:
        
eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VybmFtZSI6ImFkbWluIiwibm9uY2UiOiIyYjFkM2E2ZWI3NjczZWQ1Y2QwNTQ3NDZlOGQ5ODc2ODg0M2NjNmRkIiwiZXhwIjoxNDE5ODc3NzgzfQ.LKalETm6XoJ3jGHeSD-za8S3rIk6JsBKvRU7KSXbLJ0
     5.
        Connection:
        keep-alive
     6.
        Content-Length:
        43
     7.
        Content-Type:
        application/json
     8.
        Host:
        localhost:5000
     9.
        Origin:
        http://localhost:5000
    10.
        Referer:
        http://localhost:5000/
    11.
        User-Agent:
        Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like
        Gecko) Chrome/39.0.2171.95 Safari/537.36
 2. Request Payloadview source
    {fileName: "asdf", type: "passwdresolver"}
     1. fileName: "asdf"
     2. type: "passwdresolver"
 3. Response Headersview source
     1.
        Content-Length:
        208
     2.
        Content-Type:
        application/json
     3.
        Date:
        Mon, 29 Dec 2014 17:30:10 GMT
     4.
        Server:
        Werkzeug/0.9.6 Python/2.7.6


Thanks a lot for giving any clue.
Kind regards
Cornelius

Re: [flask] difference between request.data and request.values

From:
Daniel Neuhäuser
Date:
2014-12-29 @ 17:57
request.data is the request body as a string. request.values will contain 
the query arguments and the result of parsing request.data, provided the 
mimetype is one of the ones used for HTML forms, that is 
application/x-www-form-urlencoded or multipart/form-data.

Re: [flask] difference between request.data and request.values

From:
Cornelius Kölbel
Date:
2014-12-29 @ 18:46
Thanks for the explanaition.
So in this case I need to parse the JSON in request.data (body) myself.

I figured out, I can test it this way:

with self.app.test_request_context('/resolver/r1',
                                   data=/json.dumps/({u"resolver": u"r1",
                                         u"type": u"passwdresolver",
                                         u"fileName": PWFILE}),
                                   method='POST',
                                   headers={"Authorization": self.at}):
    res = self.app.full_dispatch_request()


Thanks a lot and kind regards
Cornelius

Am 29.12.2014 um 18:57 schrieb Daniel Neuhäuser:
> request.data is the request body as a string. request.values will 
contain the query arguments and the result of parsing request.data, 
provided the mimetype is one of the ones used for HTML forms, that is 
application/x-www-form-urlencoded or multipart/form-data.
>