“无效的凭证"在做卷曲POST时 [英] "Invalid credentials" while doing a curl POST

查看:103
本文介绍了“无效的凭证"在做卷曲POST时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下格式的卷曲请求

I have a curl request in below format

curl -v -H "Content-Type:application/json" -H "x-user-id:xxx" -H "x-api-key:yyy" --data '{"logs":"'"${TEST_OUTPUT}"'","pass":"true | false"}' https://razeedash.one.qqq.cloud.com/api/v1/clusters/zzz/api/test_results

当我从我的MAC终端执行此操作时,此方法工作正常.但是同一条命令会抛出

This works fine while I do from my MAC terminal. But the same command throws

13:49:26 {
13:49:26   "status": "error",
13:49:26   "message": "Invalid credentials"
13:49:26 }

我看到了这篇文章,但不确定我还要如何发送不带花括号的json正文?我知道我们可以将其保存为file.json并将其用作正文.但是由于某些原因,在我的方案中无法实现

I saw this post but not sure how else would I send a json body without curly braces. I know that we can save it as a file.json and use the file as body.But for some reasons that cannot be implemented in my scenario

推荐答案

通常,您应该避免尝试使用字符串插值来构建JSON.使用jq之类的工具来处理所有必要的报价.

In general, you should avoid trying to build JSON using string interpolation. Use a tool like jq to handle any necessary quoting.

jq -n --argson o "$TEST_OUTPUT" '{logs: $o, pass: "true | false"}' |
    curl -v -H "Content-Type:application/json" \
            -H "x-user-id:xxx" \
            -H "x-api-key:yyy" \
            --data @- \
            https://razeedash.one.qqq.cloud.com/api/v1/clusters/zzz/api/test_results

但是,如果您能够像现在一样正确地生成JSON,则只需将jq命令替换为echo:

However, if you can manage to correctly generate your JSON as you are now, you can just replace the jq command with echo:

echo '{"logs": ...' | curl ...

--data@-参数表示要从标准输入中读取.

The @- argument to --data says to read from standard input.

这篇关于“无效的凭证"在做卷曲POST时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆