首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >参数列表太长卷曲

参数列表太长卷曲
EN

Stack Overflow用户
提问于 2021-08-30 01:06:41
回答 2查看 641关注 0票数 1

试图解决“论点列表太长”,我一直在寻找一个解决方案,并找到了最接近我的问题的curl: argument list too long,但反应不清楚,因为我仍然有问题的“论证列表太长”。

代码语言:javascript
运行
复制
curl -X POST -d @data.txt \
   https://Path/to/attachments  \
   -H 'content-type: application/vnd.api+json' \
   -H 'x-api-key: KEY' \
-d '{
"data": {
  "type": "attachments",
  "attributes": {
    "attachment": {
    "content": "'$(cat data.txt | base64 --wrap=0)'",
    "file_name": "'"$FileName"'"
    }
  }
}
}'

谢谢

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2021-08-30 03:36:21

使用jq将base64编码的数据字符串格式化为正确的JSON字符串,然后将JSON数据作为标准输入传递给curl命令。

代码语言:javascript
运行
复制
#!/usr/bin/env sh

attached_file='img.png'

# Pipe the base64 encoded content of attached_file
base64 --wrap=0 "$attached_file" |
# into jq to make it a proper JSON string within the
# JSON data structure
jq --slurp --raw-input --arg FileName "$attached_file" \
'{
  "type": "attachments",
  "attributes": {
    "attachment": {
      "content": .,
      "file_name": $FileName
    }
  }
}
' |
# Get the resultant JSON piped into curl
# that will read the data from the standard input
# using -d @-
curl -X POST -d @- \
   'https://Path/to/attachments'  \
   -H 'content-type: application/vnd.api+json' \
   -H 'x-api-key: KEY'
票数 6
EN

Stack Overflow用户

发布于 2021-08-30 07:54:05

the linked answer

--您正试图在命令行上传递整个base64 64‘d内容

这是shell的一个限制,而不是curl。也就是说,shell是用错误argument list too long响应的。curl程序从未启动过。

建议是

curl能够加载从文件中发布的数据。

  1. 使用管道将json数据写入某个文件/tmp/data.json

(这些命令将使用管道|和文件重定向> >>,它可以处理任意数量的数据。而,you cannot place arbitrarily large amounts of data into a single command, there is a limit).

代码语言:javascript
运行
复制
echo -n '
{
"data": {
  "type": "attachments",
  "attributes": {
    "attachment": {
    "content": "' > /tmp/data.json

cat data.txt | base64 --wrap=0 >> /tmp/data.json

echo -n '",
    "file_name": "'"$FileName"'"
    }
  }
}
}' >> /tmp/data.json

  1. 使用curl命令将文件路径/tmp/data.json传递给curl命令,以便curl知道这是文件路径。

代码语言:javascript
运行
复制
curl -X POST -d @/tmp/data.json \
   "https://Path/to/attachments"  \
   -H 'content-type: application/vnd.api+json' \
   -H 'x-api-key: KEY'
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/68978047

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档