相同的curl命令不适用于bash脚本。有人能打吗
控制台尝试(成功一次(:
curl -ki
--cookie cookie-jar.txt
--header 'Content-Type: text/xml'
--request POST https://testserver:1234/v1/secondarylogin
--data '<?xml version="1.0" encoding="UTF-8"?>
<p:secondaryloginrequest xmlns:p="http://www.ericsson.com/em/am/co/authentication/v1_0/">
<credential>
<secret>6673</secret>
<type>otp</type>
</credential>
<channelinformation>COMVIVAAGENTAPP</channelinformation>
</p:secondaryloginrequest>'
**HTTP/1.1 200 OK**
Date: Wed, 02 Mar 2022 10:52:01 GMT
Content-Type: text/xml;charset=utf-8
Date: Wed, 02 Mar 2022 10:52:02 GMT
Set-Cookie: sessionid=default0c07b834d30644e9b30a9bfced82e6f2
Expires: Thu, 01 Jan 1970 00:00:00 GMT
Content-Length: 0
一个非常简单的Bash脚本(失败的一个(:
sh sectest.sh 6673
#!/usr/bin/bash
curl -ki
--cookie cookie-jar.txt
--header 'Content-Type: text/xml'
--request POST https://ugtestpo:8446/v1/secondarylogin
--data '<?xml version="1.0" encoding="UTF-8"?>
<p:secondaryloginrequest xmlns:p="http://www.ericsson.com/em/am/co/authentication/v1_0/">
<credential>
<secret>"$1"</secret>
<type>otp</type>
</credential>
<channelinformation>COMVIVAAGENTAPP</channelinformation>
</p:secondaryloginrequest>'
HTTP/1.1 401 Unauthorized
Date: Wed, 02 Mar 2022 10:47:53 GMT
Content-Type: text/xml;charset=utf-8
Content-Length: 188
我不能参数化地传递我的秘密值。
将我的评论转换为答案,以便为未来的访问者轻松找到解决方案。
以下内容应适用于允许$1
扩展的适当报价:
curl -ki
--cookie cookie-jar.txt
--header 'Content-Type: text/xml'
--request POST https://ugtestpo:8446/v1/secondarylogin
--data '<?xml version="1.0" encoding="UTF-8"?>
<p:secondaryloginrequest xmlns:p="http://www.ericsson.com/em/am/co/authentication/v1_0/">
<credential>
<secret>'"$1"'</secret>
<type>otp</type>
</credential>
<channelinformation>COMVIVAAGENTAPP</channelinformation>
</p:secondaryloginrequest>'
为了可读性,我会将xml分配给一个变量:heredocs有助于避免引用hell。
#!/usr/bin/bash
payload=$(cat <<END_XML
<?xml version="1.0" encoding="UTF-8"?>
<p:secondaryloginrequest xmlns:p="http://www.ericsson.com/em/am/co/authentication/v1_0/">
<credential>
<secret>$1</secret>
<type>otp</type>
</credential>
<channelinformation>COMVIVAAGENTAPP</channelinformation>
</p:secondaryloginrequest>
END_XML
)
curl -ki
--cookie cookie-jar.txt
--header 'Content-Type: text/xml'
--request POST
--data "$payload"
https://ugtestpo:8446/v1/secondarylogin