librelist archives

« back to archive

how to make requests to RESTful API

how to make requests to RESTful API

From:
Jeffrey Finkelstein
Date:
2011-07-08 @ 01:09
Hi,

I have a RESTful API set up with Flask to which I would like to make
GET and POST requests from my view functions. I have a "/photos" route
which accepts POST requests whose function is called "add_photo()",
and my view function looks something like this (trimmed for space):

@app.route('/add', methods=['GET', 'POST'])
def add_photos_view():
    form = PhotoUploadForm()
    if form.validate_on_submit():
        # HERE i want to make a POST request to the "/photos" route
        response = add_photo()
        # do something with the response data here...
    return render_template('add_photos.html', form=form)

Currently, as you can see in the line below the comment "# HERE ...",
I am just calling the "add_photo()" function directly. I would like
instead to have my view function to make a POST request to the
"/photos" route in a way similar to the test client:

    response = app.test_client().post('/photos', data={...})

Is there a way to programmatically make a POST request to a route
registered on a Flask application?

Jeffrey

Re: [flask] how to make requests to RESTful API

From:
Kamal Gill
Date:
2011-07-08 @ 04:20
Forget urllib2 and try Kenneth Reitz's 'requests' library -- 
http://python-requests.org/

HTH,
Kamal

On Jul 7, 2011, at 6:09 PM, Jeffrey Finkelstein wrote:

> Hi,
> 
> I have a RESTful API set up with Flask to which I would like to make
> GET and POST requests from my view functions. I have a "/photos" route
> which accepts POST requests whose function is called "add_photo()",
> and my view function looks something like this (trimmed for space):
> 
> @app.route('/add', methods=['GET', 'POST'])
> def add_photos_view():
>    form = PhotoUploadForm()
>    if form.validate_on_submit():
>        # HERE i want to make a POST request to the "/photos" route
>        response = add_photo()
>        # do something with the response data here...
>    return render_template('add_photos.html', form=form)
> 
> Currently, as you can see in the line below the comment "# HERE ...",
> I am just calling the "add_photo()" function directly. I would like
> instead to have my view function to make a POST request to the
> "/photos" route in a way similar to the test client:
> 
>    response = app.test_client().post('/photos', data={...})
> 
> Is there a way to programmatically make a POST request to a route
> registered on a Flask application?
> 
> Jeffrey

Re: [flask] how to make requests to RESTful API

From:
Jeffrey Finkelstein
Date:
2011-07-12 @ 00:04
Is there a way to accomplish this without specifying the host/port of
the running application?

Jeffrey

On Fri, Jul 8, 2011 at 12:20 AM, Kamal Gill <designbykamal@gmail.com> wrote:
> Forget urllib2 and try Kenneth Reitz's 'requests' library -- 
http://python-requests.org/
>
> HTH,
> Kamal
>
> On Jul 7, 2011, at 6:09 PM, Jeffrey Finkelstein wrote:
>
>> Hi,
>>
>> I have a RESTful API set up with Flask to which I would like to make
>> GET and POST requests from my view functions. I have a "/photos" route
>> which accepts POST requests whose function is called "add_photo()",
>> and my view function looks something like this (trimmed for space):
>>
>> @app.route('/add', methods=['GET', 'POST'])
>> def add_photos_view():
>>    form = PhotoUploadForm()
>>    if form.validate_on_submit():
>>        # HERE i want to make a POST request to the "/photos" route
>>        response = add_photo()
>>        # do something with the response data here...
>>    return render_template('add_photos.html', form=form)
>>
>> Currently, as you can see in the line below the comment "# HERE ...",
>> I am just calling the "add_photo()" function directly. I would like
>> instead to have my view function to make a POST request to the
>> "/photos" route in a way similar to the test client:
>>
>>    response = app.test_client().post('/photos', data={...})
>>
>> Is there a way to programmatically make a POST request to a route
>> registered on a Flask application?
>>
>> Jeffrey
>
>

Re: [flask] how to make requests to RESTful API

From:
Kenneth Reitz
Date:
2011-07-08 @ 04:27
You're too kind :)

--
Kenneth Reitz

On Jul 8, 2011, at 12:20 AM, Kamal Gill wrote:

> Forget urllib2 and try Kenneth Reitz's 'requests' library -- 
http://python-requests.org/
> 
> HTH,
> Kamal
> 
> On Jul 7, 2011, at 6:09 PM, Jeffrey Finkelstein wrote:
> 
>> Hi,
>> 
>> I have a RESTful API set up with Flask to which I would like to make
>> GET and POST requests from my view functions. I have a "/photos" route
>> which accepts POST requests whose function is called "add_photo()",
>> and my view function looks something like this (trimmed for space):
>> 
>> @app.route('/add', methods=['GET', 'POST'])
>> def add_photos_view():
>>   form = PhotoUploadForm()
>>   if form.validate_on_submit():
>>       # HERE i want to make a POST request to the "/photos" route
>>       response = add_photo()
>>       # do something with the response data here...
>>   return render_template('add_photos.html', form=form)
>> 
>> Currently, as you can see in the line below the comment "# HERE ...",
>> I am just calling the "add_photo()" function directly. I would like
>> instead to have my view function to make a POST request to the
>> "/photos" route in a way similar to the test client:
>> 
>>   response = app.test_client().post('/photos', data={...})
>> 
>> Is there a way to programmatically make a POST request to a route
>> registered on a Flask application?
>> 
>> Jeffrey
> 

Re: [flask] how to make requests to RESTful API

From:
Daniel , Dao Quang Minh
Date:
2011-07-08 @ 07:41
it's lovely!!! Thanks for the link.

On Fri, Jul 8, 2011 at 12:27 PM, Kenneth Reitz <me@kennethreitz.com> wrote:

> You're too kind :)
>
> --
> Kenneth Reitz
>
> On Jul 8, 2011, at 12:20 AM, Kamal Gill wrote:
>
> > Forget urllib2 and try Kenneth Reitz's 'requests' library --
> http://python-requests.org/
> >
> > HTH,
> > Kamal
> >
> > On Jul 7, 2011, at 6:09 PM, Jeffrey Finkelstein wrote:
> >
> >> Hi,
> >>
> >> I have a RESTful API set up with Flask to which I would like to make
> >> GET and POST requests from my view functions. I have a "/photos" route
> >> which accepts POST requests whose function is called "add_photo()",
> >> and my view function looks something like this (trimmed for space):
> >>
> >> @app.route('/add', methods=['GET', 'POST'])
> >> def add_photos_view():
> >>   form = PhotoUploadForm()
> >>   if form.validate_on_submit():
> >>       # HERE i want to make a POST request to the "/photos" route
> >>       response = add_photo()
> >>       # do something with the response data here...
> >>   return render_template('add_photos.html', form=form)
> >>
> >> Currently, as you can see in the line below the comment "# HERE ...",
> >> I am just calling the "add_photo()" function directly. I would like
> >> instead to have my view function to make a POST request to the
> >> "/photos" route in a way similar to the test client:
> >>
> >>   response = app.test_client().post('/photos', data={...})
> >>
> >> Is there a way to programmatically make a POST request to a route
> >> registered on a Flask application?
> >>
> >> Jeffrey
> >
>
>

Re: [flask] how to make requests to RESTful API

From:
Steven Kryskalla
Date:
2011-07-08 @ 01:56
On Thu, Jul 7, 2011 at 9:09 PM, Jeffrey Finkelstein
<jeffrey.finkelstein@gmail.com> wrote:
> Currently, as you can see in the line below the comment "# HERE ...",
> I am just calling the "add_photo()" function directly. I would like
> instead to have my view function to make a POST request to the
> "/photos" route in a way similar to the test client:
>

The request object is mutable in Flask. Try setting:

request.form = ... #whatever data you would have POSTed

And then just return the result of add_photo(). I think that will give
you the result you want. I don't think you need to use a separate API.

-Steve

Re: [flask] how to make requests to RESTful API

From:
Daniel , Dao Quang Minh
Date:
2011-07-08 @ 01:25
Have you tried `urllib2.urlopen`
http://docs.python.org/library/urllib2.html#urllib2.urlopen

On Fri, Jul 8, 2011 at 9:09 AM, Jeffrey Finkelstein <
jeffrey.finkelstein@gmail.com> wrote:

> Hi,
>
> I have a RESTful API set up with Flask to which I would like to make
> GET and POST requests from my view functions. I have a "/photos" route
> which accepts POST requests whose function is called "add_photo()",
> and my view function looks something like this (trimmed for space):
>
> @app.route('/add', methods=['GET', 'POST'])
> def add_photos_view():
>    form = PhotoUploadForm()
>    if form.validate_on_submit():
>        # HERE i want to make a POST request to the "/photos" route
>        response = add_photo()
>        # do something with the response data here...
>    return render_template('add_photos.html', form=form)
>
> Currently, as you can see in the line below the comment "# HERE ...",
> I am just calling the "add_photo()" function directly. I would like
> instead to have my view function to make a POST request to the
> "/photos" route in a way similar to the test client:
>
>    response = app.test_client().post('/photos', data={...})
>
> Is there a way to programmatically make a POST request to a route
> registered on a Flask application?
>
> Jeffrey
>