简体   繁体   English

烧瓶/前夕:带有参数的内部GET请求

[英]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. 我正在使用Flask Web服务器( Eve API的一部分),并且需要执行内部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 : 但是,当尝试从flask应用程序中的函数使用requests模块执行请求时,此方法不起作用,并引发ConnectionRefusedError

requests.exceptions.ConnectionError: ('Connection aborted.', ConnectionRefusedError(61, 'Connection refused')) requests.exceptions.ConnectionError:('连接被中止。',ConnectionRefusedError(61,'连接被拒绝'))

In order to make this request available internally , I considered to use the test_client() of my flask application as follows: 为了使该请求在内部可用 ,我考虑如下使用flask应用程序的test_client()

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. 这工作得很好,但是我需要传递其他参数,如您在第一个使用requests模块的示例代码中所见。 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

TypeError: init () got an unexpected keyword argument 'params' TypeError: init ()获得了意外的关键字参数“ params”

So is there a way to handle a GET -request either using flask or eve internally? 那么,有没有一种方法可以在内部使用flask或eve处理GET请求?

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() . 我知道有一个get_internal repo分支,用于提供与其他请求类型(如post_internal() get_internal()相当的get_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. 使用query_string关键字。

By the way, if you're posting, use the data keyword. 顺便说一句,如果您要发布,请使用data关键字。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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