试图解决“论点列表太长”,我一直在寻找一个解决方案,并找到了最接近我的问题的curl: argument list too long,但反应不清楚,因为我仍然有问题的“论证列表太长”。
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"'"
}
}
}
}'
谢谢
发布于 2021-08-30 03:36:21
使用jq
将base64编码的数据字符串格式化为正确的JSON字符串,然后将JSON数据作为标准输入传递给curl
命令。
#!/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'
发布于 2021-08-30 07:54:05
--您正试图在命令行上传递整个base64 64‘d内容
这是shell的一个限制,而不是curl
。也就是说,shell是用错误argument list too long
响应的。curl
程序从未启动过。
建议是
curl能够加载从文件中发布的数据。
/tmp/data.json
。(这些命令将使用管道|
和文件重定向>
>>
,它可以处理任意数量的数据。而,you cannot place arbitrarily large amounts of data into a single command, there is a limit).
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
curl
命令将文件路径/tmp/data.json
传递给curl
命令,以便curl知道这是文件路径。curl -X POST -d @/tmp/data.json \
"https://Path/to/attachments" \
-H 'content-type: application/vnd.api+json' \
-H 'x-api-key: KEY'
https://stackoverflow.com/questions/68978047
复制相似问题