【问题标题】:flask API to curl用于卷曲的烧瓶 API
【发布时间】:2020-05-22 03:12:10
【问题描述】:

我编写了一个烧瓶 API:

@app.route('/train/add_message/<uuid>', methods=['GET', 'POST'])
def add_message(uuid):
    content = request.get_json(silent=True)

使用python requests 库获取POST代码时

import requests

req = requests.Request('POST',myip+'train/add_message/1234',json=myjson)
prepared = req.prepare()

def pretty_print_POST(req):
    """
    At this point it is completely built and ready
    to be fired; it is "prepared".

    However pay attention at the formatting used in 
    this function because it is programmed to be pretty 
    printed and may differ from the actual request.
    """
    print('{}\n{}\r\n{}\r\n\r\n{}'.format(
        '-----------START-----------',
        req.method + ' ' + req.url,
        '\r\n'.join('{}: {}'.format(k, v) for k, v in req.headers.items()),
        req.body,
    ))

pretty_print_POST(prepared)

这是输出

-----------开始-----------

POST {ip}/train/add_message/1234
Content-Length: 347
Content-Type: application/json

b'{"type": 2, "project_id": "115", "data": [{"label150": {"example1": {"key": "hi11", "key2": "$key2"}, "example2": {"key": "hi12", "key2": "$key2"}, "example3": {"key": "hi3", "key2": "$key2"}}, "label100": {"example1": {"key": "bye1", "key2": "$key2"}, "example2": {"key": "bye2", "key2": "$key2"}, "example3": {"key": "bye3", "key2": "$key2"}}}]}'
1

如何将其转换为 curl 命令。我试过这个但出错了

curl --location --request POST myip+'/train/add__message/1234' \
--header 'Content-Type: application/x-www-form-urlencoded' \
--data-raw '{
    "type":1,
    "project_id":"313",
    "data":[{"label5":{
            "example1":{
                "key":"hi11",
                "key2":"$key2"
            },
            "example2":{
                "key":"hi12",
                "key2":"$key2"
            },
            "example3":{
                "key":"hi3",
                "key2":"$key2"
            }
        }
            }]
}'

This is the error I got:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
<title>405 Method Not Allowed</title>
<h1>Method Not Allowed</h1>
<p>The method is not allowed for the requested URL.</p>

【问题讨论】:

    标签: curl flask python-requests postman


    【解决方案1】:

    尝试添加 Accept 标头。

    curl --location --request POST myip+'/train/add__message/1234' \
    --header 'Content-Type: application/x-www-form-urlencoded' \
    --header 'Accept: application/x-www-form-urlencoded' \
    --data-raw '{
        "type":1,
        "project_id":"313",
        "data":[{"label5":{
                "example1":{
                    "key":"hi11",
                    "key2":"$key2"
                },
                "example2":{
                    "key":"hi12",
                    "key2":"$key2"
                },
                "example3":{
                    "key":"hi3",
                    "key2":"$key2"
                }
            }
                }]
    }'
    
    

    【讨论】:

    • 你能试着去掉火车前面的 / 吗?你的卷曲中有一个额外的,但在你的烧瓶应用程序中没有@MAS
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-26
    • 1970-01-01
    • 2021-06-17
    • 1970-01-01
    • 2017-08-02
    • 1970-01-01
    相关资源
    最近更新 更多