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

如何编写支持跨多行且携带-d参数的curl POST请求命令

问题根因

你编写的多行请求中,agg参数结尾的&符号与q参数之间存在换行、缩进空白字符,服务端解析application/x-www-form-urlencoded格式的请求体时,会将这些空白识别为参数名的一部分,实际解析出的参数名为\n q而非q,因此无法正常读取q参数的取值,单行写法无多余空白所以运行正常。

解决方案

方案1(推荐):拆分多个-d参数

curl会自动将多个-d传入的内容用&符号拼接,不需要手动编写拼接符,每个参数对应的JSON内容可任意换行缩进,可读性最高:

curl -X POST "localhost:8090/rest/myResource/myEndpoint?user=user01" \
 -d 'agg={
    "comm": {
        "date_histogram": {
            "field": "DERIVED_UNIFIED_TIMESTAMP",
            "calendar_interval": "month"
        }
    }
}' \
 -d 'q={
    "bool": {
        "must": [
            {
                "terms": {
                    "COMM_TYPE": [
                        "call",
                        "email",
                        "message"
                    ]
                }
            }
        ]
    }
}'

方案2:调整&符号的位置

如果需要保留单块-d参数的写法,仅需把q参数紧跟在&符号之后,不要在两者之间加换行或空格即可:

curl -X POST "localhost:8090/rest/myResource/myEndpoint?user=user01" \
 -d 'agg={
    "comm": {
        "date_histogram": {
            "field": "DERIVED_UNIFIED_TIMESTAMP",
            "calendar_interval": "month"
        }
    }
}&q={
    "bool": {
        "must": [
            {
                "terms": {
                    "COMM_TYPE": [
                        "call",
                        "email",
                        "message"
                    ]
                }
            }
        ]
    }
}'

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.25 06:24:02