Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
350 views
in Technique[技术] by (71.8m points)

How to POST a Multipart form with image and a JSON object containing an array of objects using CURL

I am trying to post the following JSON object alongside with image data using curl:

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

The best I have come so far is this:

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!

The server implementation I am trying to hit, does not recognize the objects sent with the attribute inputs. Any Idea what I am doing wrong?

Thank you very much for any direction.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You can try this

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"
  }]
}'

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share

2.1m questions

2.1m answers

62 comments

56.6k users

...