How to send JSON file as part of request body in C

2019-07-29 18:31发布

问题:

I am using CURL command line to send HTTP POST to a web service. I want to include a file's contents as a PART of the body of the POST command. Is this possible? I know I can send a file as the entire body as answered here. But I only want a part of the body to be the content of the file.

For example

curl -d '{ "name": "rahul", "speed": "fast", "data": { "number": 1, "letter": "abd", "letter2": "efg"} }' 'http://...'

Here I only want data as the file's content. Not the entire body. How can I do this?

回答1:

Set a variable to contain the file contents:

data=$(cat filename)

then substitute it into the JSON:

curl -d '{ "name": "rahul", "speed": "fast", "data": ' "$data" ' }' 'http://...'