Python 在烧瓶中获得原始柱有效载荷

Python 在烧瓶中获得原始柱有效载荷,python,python-3.x,curl,post,flask,Python,Python 3.x,Curl,Post,Flask,我用卷发发发短信 curl -X POST -d "Separate account charge and opdeducted fr" http://192.168.50.8/text 试着去 @application.route("/text",methods=['POST']) def clausIE(): content = request.data text = str(content, encoding="utf-8") 但是得到空字符串,我做错了什么 注

我用卷发发发短信

curl -X POST -d "Separate account charge and opdeducted fr" http://192.168.50.8/text
试着去

@application.route("/text",methods=['POST'])
def clausIE():
      content = request.data
      text = str(content, encoding="utf-8")
但是得到空字符串,我做错了什么


注意:我使用的是Python3.6,这不是一个真正的烧瓶问题,您使用了错误的
curl
选项

-d
开关只能用于表单数据
curl
将自动将内容类型标题设置为
application/x-www-form-urlcoded
,这意味着Flask将加载原始正文内容并将其解析为表单。您必须使用
-H'Content-Type:application/octet-stream'
或其他更适合您的数据的mime类型手动设置不同的内容类型头

您还希望使用
--data binary
,而不是
-d
--data
),因为后者还尝试将内容解析为键值字段,并将删除换行符:

curl -X POST -H 'Content-Type: application/octet-stream' \
   --data-binary "Separate account charge and opdeducted fr" \
   http://192.168.50.8/text

完整的答案似乎分散在一些评论和公认的答复中。总之,Python Flask代码应该如下所示

@application.route("/text",methods=['POST'])
def clausIE():
    content = request.get_data()

    text = str(content, encoding="utf-8") 

    return text
这是你在终点站应该有的

curl -X POST --data-binary "Hello World!" http://192.168.50.8/text
在我自己的设置(OSX)中,我可以删除
-xpost
,并将URL用引号括起来,这样就可以

curl --data-binary "Hello World!" "http://192.168.50.8/text"

嗯,您需要使用
request.get_data()
而不是
request.data
返回第e条中的某些内容。