首页 > 解决方案 > 如何在烧瓶 python 应用程序中执行 curl POST 命令

问题描述

我在一个小烧瓶应用程序上工作,该应用程序包括使用关键字(ElasticSSearch)搜索文档并在集群中上传文档。

搜索文档工作正常,但上传很难,我可以用 curl 做,但我有一些问题

我的 curl 命令是这样的:

curl -F "file=@pdf-test.pdf" "http://127.0.0.1:pdf-8080/fscrawler/_upload"

我的html是:

<form id="uploadbanner" enctype="multipart/form-data" method="post" action="/">
   <input id="fileupload" name="myfile" type="file" />
   <input type="submit" value="submit" id="submit" />
</form>

为了为客户端创建上传按钮。

我看到了很多关于请求库的话题,但是我的 py 代码不起作用,我真的不明白我做错了什么,我很迷茫。

我给你我试过的:

def upload():
url="http://127.0.0.1:8080/fscrawler/_upload"
    if request.method == 'POST':
        payload = open("test.pdf")

        r = requests.post(url, data=payload)

感谢您的帮助,祝您有美好的一天:)

标签: pythoncurlflask

解决方案


Try:

curl -F "myfile=@pdf-test.pdf" "http://127.0.0.1:pdf-8080/fscrawler/_upload"

The name of your file input needs to be the same in curl and html.


推荐阅读