【问题标题】:argument list too long curl参数列表太长 curl
【发布时间】:2021-10-28 20:20:48
【问题描述】:

试图解决“参数列表太长” 我一直在寻找解决方案,并找到了最接近我的问题的解决方案 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"'"
    }
  }
}
}'

谢谢

【问题讨论】:

  • 是的,那里的答案可能需要一些充实。基本上,您需要以与第一个相同的方式处理第二个数据块:将其放入文件中,然后使用-d @filename 告诉curl 从该文件中读取它。
  • 没用的cat 改用base64 --wrap=0 <data.txt
  • 根据链接的答案“您正在尝试在命令行上传递全部 base64 内容”。这是 shell 的限制,而不是 curl。建议是“curl 能够从文件加载数据到 POST”。将json数据写入某个文件/tmp/data,然后使用@将该文件路径传递给curl,这样curl就知道这是一个文件路径curl -d @/tmp/data ...curl 将从文件/tmp/data 中读取数据。

标签: bash curl


【解决方案1】:

使用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'

【讨论】:

    【解决方案2】:

    the linked answer

    您正在尝试在命令行上传递全部 base64 内容

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

    建议是

    curl 能够从文件中加载数据到 POST

    1. 使用管道将json数据写入某个文件/tmp/data.json
      (命令将使用管道|和文件重定向>>>可以处理任意大量数据。而@987654322 @)。
    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. 使用@ 将该文件路径/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'
    

    【讨论】:

      猜你喜欢
      • 2019-06-03
      • 2019-02-22
      • 1970-01-01
      • 2017-11-30
      • 2020-05-09
      • 2014-05-21
      • 2015-04-04
      • 2021-10-26
      • 2014-04-18
      相关资源
      最近更新 更多