如何在CURL POST命令中将JSON文件作为请求正文的一部分发送 [英] How to send JSON file as part of request body in CURL POST command
本文介绍了如何在CURL POST命令中将JSON文件作为请求正文的一部分发送的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用CURL命令行将HTTP POST发送到Web服务.我想将文件内容作为POST命令正文的 PART 包含在内.这可能吗?我知道我可以按照此处的方式发送整个文件.但是我只希望正文的一部分成为文件的内容.
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.
例如
curl -d '{ "name": "rahul", "speed": "fast", "data": { "number": 1, "letter": "abd", "letter2": "efg"} }' 'http://...'
在这里,我只想将data
作为文件的内容.不是整个身体.我该怎么办?
Here I only want data
as the file's content. Not the entire body. How can I do this?
推荐答案
设置一个变量以包含文件内容:
Set a variable to contain the file contents:
data=$(cat filename)
然后将其替换为JSON:
then substitute it into the JSON:
curl -d '{ "name": "rahul", "speed": "fast", "data": ' "$data" ' }' 'http://...'
这篇关于如何在CURL POST命令中将JSON文件作为请求正文的一部分发送的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文