You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Flask POST端点使用curl发起请求时无法接收参数如何解决?

问题原因

Flask 中不同的参数获取方法对应不同的参数传递位置:

  • request.args 专门用于获取 URL 查询字符串参数,也就是请求地址中 ? 后面拼接的 key=value 格式参数
  • 你使用 curl 的 -d 参数传递的内容会放在 HTTP 请求体中,默认是 application/x-www-form-urlencoded 格式的表单参数,这类参数需要通过 request.form 获取

你的代码中判断 start 是否存在于 request.args,自然找不到通过请求体传递的参数,因此始终返回 missing args。

解决方法

你可以根据自己的使用场景二选一:

方案1:修改后端代码适配当前 curl 请求

保留现有 curl 传参逻辑,修改路由中的参数获取方式即可:

from flask import Flask
from flask import request

def create_app():
    app = Flask(__name__)

    @app.route("/shipment", methods=["POST"])
    def hello_world():
        # 改为从 request.form 中获取表单参数
        if "start" in request.form:
            return "arg found"
        else:
            return "missing args"

    return app

方案2:修改 curl 请求适配当前后端代码

保留现有后端逻辑,把参数放到 URL 查询字符串中传递,修改后的 curl 命令如下:

curl -X POST "127.0.0.1:5000/shipment?start=test"

扩展:JSON 格式参数的获取方式

如果你后续需要传递 JSON 格式参数,curl 请求需要指定请求头,示例如下:

curl -X POST 127.0.0.1:5000/shipment -H "Content-Type: application/json" -d '{"start":"test"}'

对应的后端参数获取逻辑为:

# 先解析请求体中的JSON,再获取对应字段
data = request.get_json()
if data and "start" in data:
    return "arg found"

内容的提问来源于stack exchange,提问作者Mar

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.24 14:36:10