【问题标题】:Why is functions-framework returning a 400 status code when i send it a file?为什么函数框架在我发送文件时返回 400 状态代码?
【发布时间】:2021-10-08 19:17:54
【问题描述】:

我正在笔记本电脑上本地开发一个微服务架构,其中一个烧瓶应用程序与谷歌云功能通信(使用functions-framework 库)。

在其中,会发生以下情况:

  • 用户将文件上传到表单。提交后,文件将发送到 Flask
  • Flask 将从请求中提取文件,然后将文件发送到云函数,在端口 8080 上运行

通过云函数发送文件时返回 400 状态代码(错误请求)。

我做错了什么?

===form.html===

<html>
   <body>
   <h1>Report</h1>

      <form action = "http://localhost:3000**/process**" method = "post" enctype = "multipart/form-data">
     <input type = "file" name = "file" />
    <input type = "hidden" name = "report" />
         <p><input type = "submit" value = "submit" /></p>
      </form>   
   </body>
</html>

===烧瓶===

...

@app.route('/process',methods = ['POST'])
def process():
      f = request.files['file']
      files = {'document': f.read()}
      headers = {'Content-type': 'multipart/form-data'}
      url='http://127.0.0.1:8080'
      r = requests.post(url,files=files,headers=headers)
      if r:
         return redirect(url_for('success',tool=r.text))

@app.route('/form')
def form():
   return render_template('form.html')


if __name__ == '__main__':
    app.run(port=3000)

===函数框架===

def func2(request):
    file=request.files['document']
    return str(type(file))

【问题讨论】:

  • 您能否说明如果您向端口 8080 发送 CURL 请求,针对 Functions Framework 托管代码的 CURL 请求是否有效...func2()
  • 感谢@Kolban,我一直在使用 /process 视图发送帖子请求:url='http://127.0.0.1:8080' r = requests.post(url,files=files,headers=headers) 我也尝试使用 curl 但又得到了 400?

标签: python python-requests google-cloud-functions functions-framework


【解决方案1】:

documentation for Functions Framework 中,有更多链接可以了解更多信息,尤其是how to use the Functions Framework for Python runtime。在这里你可以找到quickstart about error handling

在这个来自另一个问题的answer 中,您可以检查如何为给定代码错误实现错误处理程序并获取错误描述,以便调试您的代码并了解为什么您会收到 400 状态代码错误。

【讨论】:

    猜你喜欢
    • 2022-08-06
    • 1970-01-01
    • 2014-10-26
    • 1970-01-01
    • 2021-10-23
    • 2017-04-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多