使用python发送带有参数的发布请求

时间:2020-07-29 09:45:55

标签: python python-3.x python-requests werkzeug

我已经使用werkzeug创建了发布路线。

http:// localhost:8000 / v1 / api / /数据

def url_map():
    tenants = [
        Submount(
            "/<serial_id>",
            [
                Rule(
                    "/data",
                    methods=["POST"],
                    endpoint=getData,
                )
            ],
        )
    ]

    api = [Submount("/api", api)]

    rules = [Submount("/v1", api)]
    return Map(rules, strict_slashes=False)


   def getData(self, request, serial_id):
        logger.error('88888888888888888888888888888888888888888888888888888888888')
        logger.error(serial_id)
        return {'status': 'ok'}

我正在向路径发送请求:

requests.post('http://localhost:8000/v1/api/<serial_id>/data',
                                                data= json.dumps({'data':'data'}),
                                                params={'serial_id':1}
                                                )

问题在于,不是打印1而是将serial_id打印为

Expected is:
88888888888888888888888888888888888888888888888888888888888
1

Actual is:
88888888888888888888888888888888888888888888888888888888888
<serial_id>

1 个答案:

答案 0 :(得分:1)

@Md Jewele Islam在评论中指出,url变量必须类似于:

url = 'http://localhost:8000/v1/api/{}/data'.format(str(serial_id))

,并且必须按照以下方式发送请求:

import json
res = requests.post(url , data= json.dumps({'data':'data'}), params={'serial_id':1})

因此您可以通过以下方式打印响应:

print(res.text)