Why is functions-framework returning a 400 status code when i send it a file?

1.1k Views Asked by At

I am developing a micro services architecture locally on my laptop, where a flask app communicates with a Google cloud function (using functions-framework library).

In it,the following happens:

  • The user uploads a file to a form. Upon submit, the file is sent to Flask
  • Flask will then extract the file from the request and then send the file to the cloud function, running on port 8080

When the file is sent though the cloud function is returning a 400 status code (bad request).

What am I doing wrong?

===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>

===Flask===

...

@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)

===functions-framework===

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

There are 1 best solutions below

0
On

In the documentation for Functions Framework, there are more links to learn more about this, particularly on how to use the Functions Framework for Python runtime. Here you can find a quickstart about error handling.

In this answer from another question, you could check how to implement an error handler for a given code error and get the description of the error in order to debug your code and know why you're getting the 400 status code error.