我如何在Shell脚本中卷曲带有标头的请求 [英] How could I curl for a request with headers in shell scripting
问题描述
我尝试如下执行CURL语句,对此我需要得到响应:
I tried to execute a CURL statement as follows, for which I am getting required response:
curl -s -POST --header 'Content-Type: application/json' 'http://www.dummy.com/projectname/page_relevance' -d '{"query": "q_string", "results": [{"abstract": "abs_string", "title": "title_string"}, "mode": "value", "cache": true, "source": "value"}'
但是当我尝试将变量值传递给参数"query"时,下面提到的curl语句不起作用,并在响应中观察到一些错误语句:
But when i tried to pass variable values to parameter "query", the curl statement mentioned below not works and observed some error statement in response:
curl -s -POST --header 'Content-Type: application/json' 'http://www.dummy.com/projectname/page_relevance' -d '{"query": "$query_string", "results": [{"abstract": "abs_string", "title": "title_string"}, "mode": "value", "cache": true, "source": "value"}'
ERROR语句:
未找到[CFN#0005]
有效!
这是此服务器的默认网页.
Not Found [CFN #0005]
It works!
This is the default web page for this server.
Web服务器软件正在运行,但尚未添加任何内容.
The web server software is running but no content has been added, yet.
但是请确保我用变量第二次构造的curl请求类似于我最初执行的同一个curl请求.使用echo将$ query_string替换为正确的值对此进行了测试.
But am sure that curl request which i constructed second with variable resembles the same curl request which i executed at first. This is tested using echo which replaces $query_string with correct value.
我还尝试了另一种方法,其中我没有对单个参数使用任何变量,而是尝试如下操作:
I also tried in another approach, in which i have not used any variables for single parameter, instead i tried as below:
a='{"query": "query_value", "results": [{"abstract": "abs_string", "title": "title_string"}, "mode": "value", "cache": true, "source": "value"}'
curl -s -POST --header 'Content-Type: application/json' 'http://www.dummy.com/projectname/page_relevance' -d $a
我还尝试使用${a}
,"$a"
,'$a'
仍然观察到相同的错误.
still same error is been observed.
推荐答案
单引号'
(您在-d
参数中使用)保留每个字符的文字值,包括$
(请参阅这样的答案),这就是为什么变量query_string
是没有被扩展.
Single quotes '
(you're using in -d
argument) preserve the literal value of each character, including the $
(see this SO answer), and that's why your variable query_string
is not being expanded.
尝试一下:
~$ query_string="my query"
~$ echo '$query_string'
$query_string
~$ echo "$query_string"
my query
因此,如果希望变量扩展到其值,则需要使用双引号"
.
So, you need to use double quotes "
if you wish your variables to expand to its values.
但是,为了像在JSON数据中一样嵌套双引号(在其他双引号内),您必须:
However, in order to nest double quotes (inside other double quotes), as in you JSON data, you must either:
-
转义内引号,如下所示:
~$ echo "{\"query\": \"$query_string\"}"
{"query": "my query"}
但是很快就会变得很丑陋.或
but that gets very ugly, very soon; or
串联字符串用单引号和双引号引起来,如下所示:
concatenate strings under alternating single and double quotes, like this:
~$ echo '{"query": "'"$query_string"'"}"'
{"query": "my query"}"
对于较短的字符串可能更易读;或
which may be more readable for shorter strings; or
使用此处文档:
~$ read query <<-END
{"query": "$query_string"}
END
~$ echo "$query"
{"query": "my query"}
此处的文档特别适用于较长的文档,其中您希望进行参数/变量扩展,命令替换,算术扩展等.
Here-documents are particularly convenient for longer documents in which you wish for parameter/variable expansion, command substitution, arithmetic expansion, etc.
总而言之,使用上述方法之一定义了JSON查询后(也许通过here-document),您可以像下面这样编写curl
命令:
In summary, after defining your JSON query with one of the above ways (perhaps via a here-document), you can write your curl
command like this:
curl -s -X POST -H 'Content-Type: application/json' 'http://www.dummy.com/projectname/page_relevance' -d "$query"
这篇关于我如何在Shell脚本中卷曲带有标头的请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!