How do I add a key when posting a image?

I have a issue when posting a file to a server developed by python flask, but always failed with a key error that the key "file" cannot found in request. My question is how to add the key "file" in web post file method?
here is the blocks:
微信截图_20211228111915

here is the code in server:


You can see that in the server post method, it will get a file object from request.files["file"], but the request does not has this key.

Here is the response error:

400 Bad Request: The browser (or proxy) sent a request that this server could not understand. KeyError: 'file'

I also captured the request raw data:

  • Post from inventor:

Welcome any suggestions. Thanks.

Is the server expecting multipart? You cannot send multipart with the web component.

https://pythonbasics.org/flask-upload-file/

Thanks, so any workaround recommend?

Use php instead ?

For example:

It may also be that using the customWebView you can upload your files...

If I'm not mistaken, there are extensions out there that can encode images in base64. You may consider using such a method to "stringify" your image before POSTing it as text.

the request header must be a list of key value pairs, i.e. a list of lists...
you currently only have a key value pair, i.e. a simple list

Taifun

1 Like

Many thanks for the suggestions. I resolved this issue by updating the python server code. use "request.data" to replace with "request.files['file']", after that, key is not required when posting any more.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.