【问题标题】:Stream the JSON Results of Curl into Data Parameter of Curl将 Curl 的 JSON 结果流式传输到 Curl 的数据参数中
【发布时间】:2019-03-30 13:03:55
【问题描述】:

我正在尝试将 JSON 输出从一个 curl 流式传输到另一个 curl 的数据参数中。可能吗?

第一步:例如输出JSON的curl为:

curl -sG api.com/test -H "Authorization: Bearer ${token}" | jq '.[] | select(.query|test("latency")) | @json'

返回:

"{\"query\":\"origin == 'router' and name == 'latency'\",\"threshold\":{\"critical\":65000,\"warning\":55000,\"type\":\"UPPER\"}}"

第 2 步:如果我在第二个 curl 中使用第一个 curl 的输出,我有一个成功的响应:

curl -v "api.com/test" --data "{\"query\":\"origin == 'router' and name == 'latency'\",\"threshold\":{\"critical\":20000,\"warning\":15000,\"type\":\"UPPER\"}}" -H "Authorization: Bearer ${token}" -H "Content-Type: application/json"

成功响应:

{
  "query" : "origin == 'router' and name == 'latency'",
  "threshold" : {
    "critical" : 20000.0,
    "type" : "UPPER",
    "warning" : 15000.0
  }
}

第 1 步和第 2 步合并:我正在尝试将这两个步骤合并为一个,如下所示:

curl -sG hapi.com/test -H "Authorization: Bearer ${token}" | jq '.[] | select(.query|test("latency")) | @json' | curl -d @- -H "Authorization: Bearer ${token}" -H "Content-Type: application/json" "api.com/test"

但是不断收到错误的请求:

{
  "timestamp" : 1553950852034,
  "status" : 400,
  "error" : "Bad Request",
  "exception" : "org.springframework.http.converter.HttpMessageNotReadableException",
  "message" : "Bad Request",
  "path" : "/test"
}

可能吗?

【问题讨论】:

  • INSTANCE_REGION=$(curl -sG api.com/test -H "授权:承载 ${token}" | jq '.[] | select(.query|test("latency")) | @json') | curl -v "api.com/test" --data $INSTANCE_REGION -H "Authorization: Bearer ${token}" -H "Content-Type: application/json"
  • 返回HTTP/1.1 401 Unauthorized
  • 必须是您的身份验证标头。
  • 也许吧。我确实确保令牌是有效的。
  • 您是否尝试过不将select 过滤器的输出通过管道传输到@json

标签: json bash curl input stream


【解决方案1】:

您在第一个命令的输出中看到的引号是文字 引号,由@json 添加。它们与您在第二个示例中使用的引号不同,后者只是 句法 引号,用于保护内容免受 shell 扩展。

当直接从第一个curl 到第二个时,不需要语法引号,因为shell 永远不会看到数据。您需要删除 @json 过滤器,该过滤器将您的回复用引号括起来。

auth="Authorization: Bearer ${token}"
curl -sG hapi.com/test -H "$auth" |
  jq '.[] | select(.query|test("latency"))' |
  curl -d @- -H "$auth" -H "Content-Type: application/json" "api.com/test"

【讨论】:

    【解决方案2】:

    你可以试试

    curl -v "api.com/test" --data "`curl -v -sG api.com/test -H "Authorization: Bearer ${token}" | jq '.[] | select(.query|test("latency")) | @json'`"
    

    【讨论】:

    • 试过这个仍然得到 400 响应。
    • 你能发布带有'-v'标志的实际输出吗?
    • * 正在尝试 IP... * TCP_NODELAY 设置 * 已连接到 api.com/test (IP) 端口 80 (#0) > POST /v1/alert-configurations HTTP/1.1 > 主机:api。 com > User-Agent: curl/7.54.0 > Accept: / > Authorization: Bearer > Content-Type: application/json > Content-Length: 136 > * 上传完全发送: 136 个字节中的 136 个
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-28
    • 2019-05-15
    • 1970-01-01
    • 1970-01-01
    • 2021-11-12
    相关资源
    最近更新 更多