在请求上下文之外工作-Flask / Guzzle

时间:2019-02-25 16:37:04

标签: flask request guzzle

我正在通过Laravel上传文件,并正在使用Guzzle进行帮助。该函数如下所示:

$client = new \GuzzleHttp\Client();
        $response = $client->request('POST', 'url', [
            'multipart' => [[
                'name'     => 'file',
                'contents' => $file,
                'filename' => $save_name,
                ]]
            ]);
        $response = json_decode($response->getBody(),true);
        dd($response);

这将发送到Lambda函数,该函数是Flask的一个小应用程序,为了询问这个问题,它已简化为绝对裸露的骨骼,同时仍会触发错误。

from flask import Flask, request

app = Flask(__name__)
@app.route('/',methods=['POST'])

def main():
    start = time.time()
    data = request.files['file']
    return data.name

if __name__ == '__main__':
    app.run(port=5000, debug=True)

这实际上在我dd进行筛选时输出完美,但是,当我查看尾部日志时,会出现如下所示的不间断错误:

[1551111648511] Working outside of request context.

This typically means that you attempted to use functionality that needed
an active HTTP request.  Consult the documentation on testing for
information about how to avoid this problem.: RuntimeError
Traceback (most recent call last):
  File "/var/task/handler.py", line 580, in lambda_handler
  return LambdaHandler.lambda_handler(event, context)
  File "/var/task/handler.py", line 248, in lambda_handler
  return handler.handler(event, context)
  File "/var/task/handler.py", line 424, in handler
  result = self.run_function(app_function, event, context)
  File "/var/task/handler.py", line 283, in run_function
  result = app_function(event, context) if varargs else app_function()
  File "/var/task/main.py", line 31, in main
  if request.method == 'POST':
  File "/var/task/werkzeug/local.py", line 347, in __getattr__
  return getattr(self._get_current_object(), name)
  File "/var/task/werkzeug/local.py", line 306, in _get_current_object
  return self.__local()
  File "/var/task/flask/globals.py", line 37, in _lookup_req_object
  raise RuntimeError(_request_ctx_err_msg)
RuntimeError: Working outside of request context.

This typically means that you attempted to use functionality that needed
an active HTTP request.  Consult the documentation on testing for
information about how to avoid this problem.

阅读其他答案后,我尝试插入

with app.test_request_context:
    data = request.files['file']

但是,这导致了错误的请求。

如果我从本地计算机发送了一个带有附加文件的curl请求,它会正常运行且没有错误,因此我想知道这是否与guzzle有关。

一如既往,感谢所有帮助!

0 个答案:

没有答案