在bash脚本中运行时,为什么curl会返回“Usage:curl [options...] <url>提供的类别无效”?

问题描述 投票:0回答:1

我正在尝试在 .sh 文件中创建一个 POST 请求,该请求会发布存储在变量中的一些 json。在 POST 之后,它应该存储帖子的响应,以便我通过 echo 检查。我正在使用curl 7.81.0 的管道中执行此操作。当我运行代码时,我得到:

用法:curl [options...] 提供的类别无效,这里是所有类别的列表: auth 不同类型的身份验证方法连接 低级网络操作curl 命令行工具本身 dns 常规 DNS 选项文件 FILE 协议选项 ftp FTP 协议options http HTTP 和 HTTPS 协议选项 imap IMAP 协议 options Misc 不属于任何其他类别的选项 output 文件系统输出 pop3 POP3 协议选项 post HTTP Post 特定选项 proxy 所有与代理相关的选项 scp SCP 协议选项 sftp SFTP 协议选项 smtp SMTP协议选项 ssh SSH 协议选项 telnet TELNET 协议选项 tftp TFTP 协议选项 tls 所有 TLS/SSL 相关选项 upload 所有上传选项 verbose 与任何种类的 curl 命令行输出相关的选项

我有一个在有效文件中较早发生的curl Get 请求。

test=$(curl -X GET "https://test.com/test/test" -u username:password | jq '{id, things}' > test.json)

当我发表类似的帖子时,我遇到了问题:

{
  jq -cn  --arg url $url  --arg key $key '{things: { stuff: $ARGS.named}}'
} | jq -s add > newTest.json

mergedTest=$(jq -s '.[0] * .[1]' test.json newTest.json)

test2=$(curl -X POST https://test.com/test/test -u username:password -h "Content-Type: application/json" --data $mergedTest)
echo $test2

如有任何帮助,我们将不胜感激。

bash curl post jq
1个回答
0
投票

尝试将数据放在引号中:

{
  jq -cn  --arg url $url  --arg key $key '{things: { stuff: $ARGS.named}}'
} | jq -s add > newTest.json

mergedTest=$(jq -s '.[0] * .[1]' test.json newTest.json)

test2=$(curl -X POST https://test.com/test/test -u username:password -h "Content-Type: application/json" --data "$mergedTest")
echo $test2
© www.soinside.com 2019 - 2024. All rights reserved.