librelist archives

« back to archive

Failing to get POST data from request.data

Failing to get POST data from request.data

From:
Christophe Meessen
Date:
2013-03-03 @ 18:25
Hello,

I have a javascript program that submit a POST request to a Flask 
application with a string data in the request.
For this I tried both of the following commands:

$.ajax( {type: 'POST', url: thisId+'/moveUp', data : 'test' } );
$.post( thisId+'/moveUp', 'test' );

Looking at firefox console web, I see that 4 bytes of data have been 
sent. I couldn't find how to see their value though.

In the Flask application I have this route and handler

@app.route('/list/<passwd>/<int:itemId>/moveUp', methods=['POST'])
def listItemMoveUp(passwd, itemId):
     app.logger.debug("'" + request.data + "'")
     g.db.moveShopId(passwd, itemId, int(request.data))

The problem I have is that request.data is an empty string while I 
expected it to contain 'test'.
It shows up as
--------------------------------------------------------------------------------
DEBUG in __init__ [/home/meessen/Sources/shopList/app/__init__.py:36]:
''
--------------------------------------------------------------------------------

Of course 'test' would be replaced with a meaningful value when it 
works. I know I could pass the value in the url, but I would like to 
understand why I can't access the data passed with the POST request.
Is there another way to access the posted data ?

-- 
Bien cordialement,

Ch. Meessen

Re: [flask] Failing to get POST data from request.data

From:
Weslly Honorato
Date:
2013-03-03 @ 18:29
If you want to get "POST" data you should access the request.form attributes

On Mar 3, 2013, at 3:25 PM, Christophe Meessen <christophe@meessen.net> wrote:

> Hello,
> 
> I have a javascript program that submit a POST request to a Flask 
> application with a string data in the request.
> For this I tried both of the following commands:
> 
> $.ajax( {type: 'POST', url: thisId+'/moveUp', data : 'test' } );
> $.post( thisId+'/moveUp', 'test' );
> 
> Looking at firefox console web, I see that 4 bytes of data have been 
> sent. I couldn't find how to see their value though.
> 
> In the Flask application I have this route and handler
> 
> @app.route('/list/<passwd>/<int:itemId>/moveUp', methods=['POST'])
> def listItemMoveUp(passwd, itemId):
>     app.logger.debug("'" + request.data + "'")
>     g.db.moveShopId(passwd, itemId, int(request.data))
> 
> The problem I have is that request.data is an empty string while I 
> expected it to contain 'test'.
> It shows up as
> --------------------------------------------------------------------------------
> DEBUG in __init__ [/home/meessen/Sources/shopList/app/__init__.py:36]:
> ''
> --------------------------------------------------------------------------------
> 
> Of course 'test' would be replaced with a meaningful value when it 
> works. I know I could pass the value in the url, but I would like to 
> understand why I can't access the data passed with the POST request.
> Is there another way to access the posted data ?
> 
> -- 
> Bien cordialement,
> 
> Ch. Meessen
> 

Re: [flask] Failing to get POST data from request.data

From:
Dave Shawley
Date:
2013-03-03 @ 18:55
On Mar 3, 2013, at 1:25 PM, Christophe Meessen <christophe@meessen.net> wrote:

> 
> $.ajax( {type: 'POST', url: thisId+'/moveUp', data : 'test' } );
> $.post( thisId+'/moveUp', 'test' );
> 

When you send a POST message, jQuery will default to sending the data as 
"application/x-www-form-urlencoded".  Sending "test" as the data is like 
posting a form with a single, valueless parameter named test.  IIRC, you 
can set the content type to something like "application/octet-stream" and 
it will send the data unencoded in the body.

Enjoy, Dave.
--

Re: [flask] Failing to get POST data from request.data

From:
Christophe Meessen
Date:
2013-03-03 @ 21:48
Thank you very much. With $.ajax I could specify the contentType and it worked.

With $.post it doesn't seam to be possible to specify the content type. 
From the documentation we are supposed to be able to send a string or 
specify a data type. But considering the ajax equivalent code given in the
documentation, I suppose it is always sent as 
application/x-www-form-urlencoded. So apparently I should better stick 
with the $.ajax function and use $.post only if the data sent is 
x-www-form-urlencoded. Is that right ?


--
Ch.Meessen

Le 3 mars 2013 à 19:55, Dave Shawley <daveshawley@gmail.com> a écrit :

> On Mar 3, 2013, at 1:25 PM, Christophe Meessen <christophe@meessen.net> wrote:
> 
>> 
>> $.ajax( {type: 'POST', url: thisId+'/moveUp', data : 'test' } );
>> $.post( thisId+'/moveUp', 'test' );
>> 
> 
> When you send a POST message, jQuery will default to sending the data as
"application/x-www-form-urlencoded".  Sending "test" as the data is like 
posting a form with a single, valueless parameter named test.  IIRC, you 
can set the content type to something like "application/octet-stream" and 
it will send the data unencoded i n the body.
> 
> Enjoy, Dave.
> --
> 

Re: [flask] Failing to get POST data from request.data

From:
Carsten Brueggenolte
Date:
2013-03-26 @ 06:17
DVDs sd
—
Sent from a mobile phone

On Sun, Mar 3, 2013 at 7:56 PM, Dave Shawley <daveshawley@gmail.com>
wrote:

> On Mar 3, 2013, at 1:25 PM, Christophe Meessen <christophe@meessen.net> wrote:
>> 
>> $.ajax( {type: 'POST', url: thisId+'/moveUp', data : 'test' } );
>> $.post( thisId+'/moveUp', 'test' );
>> 
> When you send a POST message, jQuery will default to sending the data as
"application/x-www-form-urlencoded".  Sending "test" as the data is like 
posting a form with a single, valueless parameter named test.  IIRC, you 
can set the content type to something like "application/octet-stream" and 
it will send the data unencoded in the body.
> Enjoy, Dave.
> --

Re: [flask] Failing to get POST data from request.data

From:
Carsten Brueggenolte
Date:
2013-03-26 @ 06:17
Cs
—
Sent from a mobile phone

On Sun, Mar 3, 2013 at 7:56 PM, Dave Shawley <daveshawley@gmail.com>
wrote:

> On Mar 3, 2013, at 1:25 PM, Christophe Meessen <christophe@meessen.net> wrote:
>> 
>> $.ajax( {type: 'POST', url: thisId+'/moveUp', data : 'test' } );
>> $.post( thisId+'/moveUp', 'test' );
>> 
> When you send a POST message, jQuery will default to sending the data as
"application/x-www-form-urlencoded".  Sending "test" as the data is like 
posting a form with a single, valueless parameter named test.  IIRC, you 
can set the content type to something like "application/octet-stream" and 
it will send the data unencoded in the body.
> Enjoy, Dave.
> --