简体   繁体   中英

Flask / Eve: Internal GET request with parameters

I am using a flask webserver (part of an eve api) and need to do an internal GET -request.

For an external request I use this code:

url = 'http://127.0.0.1:5000/simulations'
r = requests.get(url,
                 headers={'Content-Type': 'application/json', 'Accept': 'application/json'},
                 params={'hateoas': False}
                 )

However, this is not working when trying to execute the request using the requests module from a function within the flask app and raises an ConnectionRefusedError :

requests.exceptions.ConnectionError: ('Connection aborted.', ConnectionRefusedError(61, 'Connection refused'))

In order to make this request available internally , I considered to use the test_client() of my flask application as follows:

r = app.test_client().get(url,
                          headers={'Content-Type': 'application/json', 'Accept': 'application/json'}
                          )

This is working pretty well, but I need to pass additional parameters as you can see in my first sample snippet using the requests module. So I extended the code above to this:

r = app.test_client().get(url,
                          headers={'Content-Type': 'application/json', 'Accept': 'application/json'},
                          params={'hateoas': False}
                          )

Unfortunately, this raises a TypeError :

TypeError: init () got an unexpected keyword argument 'params'

So is there a way to handle a GET -request either using flask or eve internally?

I know that there is a get_internal repo branch for eve delivering a development implementation of get_internal() comparable to the other request types like post_internal() . But I do not like to use this development branch since it is not sure whether this feature would be implemented in the next stable release.

The way you pass the query string is not right, you should:

r = app.test_client().get(url,
                          headers={'Content-Type': 'application/json', 'Accept': 'application/json'},
                          query_string={'hateoas': False})

use the query_string keyword.

By the way, if you're posting, use the data keyword.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM