【问题标题】:How to POST a Multipart form with image and a JSON object containing an array of objects using CURL如何使用 CURL 发布带有图像和包含对象数组的 JSON 对象的 Multipart 表单
【发布时间】:2021-01-11 06:05:56
【问题描述】:

我正在尝试使用 curl 将以下 JSON 对象与图像数据一起发布:

{
  "score_id": "153A6D67",
  "inputs": [{
    "type": "hits",
    "value": "4"
  },{
    "type": "miss",
    "value": "3"
  }]
}

到目前为止,我取得的最好成绩是:

curl --request POST "https://my-server/post-url" \
--header 'Accept: application/json' \
--form "score_id=153A6D67" \
--form 'inputs[]={"type":"hits","value":"4"}; type=application/json' \
--form 'inputs[]={"type":"miss","value":"3"}; type=application/json' \
--form "uploaded_image=@$IMAGE"   # <<< IMPORTANT! this is an image!

我尝试访问的服务器实现无法识别使用属性inputs 发送的对象。 知道我做错了什么吗?

非常感谢您的任何指导。

【问题讨论】:

    标签: arrays json shell curl


    【解决方案1】:

    你可以试试这个

    curl --location --request POST 'https://my-server/post-url' \
    --header 'Content-Type: application/json' \
    --data-raw '{
      "score_id": "153A6D67",
      "inputs": [{
        "type": "hits",
        "value": "4"
      },{
        "type": "miss",
        "value": "3"
      }]
    }'
    

    【讨论】:

    • 感谢您的回复,但不允许使用多部分表单!查看我的最后一行:--form "uploaded_image=@$IMAGE"
    • 将图片字符串作为base64编码格式传递
    【解决方案2】:

    这就是我让它工作的方式:

    curl --request POST "https://my-server/post-url" \
    --header 'Accept: application/json' \
    --form "score_id=153A6D67" \
    --form 'inputs[0][type]=hits' \
    --form 'inputs[0][value]=4' \
    --form 'inputs[1][type]=miss' \
    --form 'inputs[1][value]=3' \
    --form "uploaded_image=@$IMAGE" 
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-04-11
      • 1970-01-01
      • 2011-02-13
      • 1970-01-01
      • 1970-01-01
      • 2015-12-09
      • 2019-06-10
      • 2014-07-16
      相关资源
      最近更新 更多