我正在使用curl
命令,它使用了很多变量。这些变量包含XML。
我的主要XML文件是:
<?xml version="1.0" encoding="UTF-8"?>
<md:EntityDescriptor ID="_16x6ttxhv1vzbzoop6nddfaece4r9ota2tem7as" entityID="http://jenkins-0.com:8080/securityRealm/finishLogin" validUntil="2038-10-24T23:16:50.279Z" xmlns:md="urn:oasis:names:tc:SAML:2.0:metadata"><md:Extensions xmlns:alg="urn:oasis:names:tc:SAML:metadata:algsupport"><alg:DigestMethod Algorithm="http://www.w3.org/2001/04/xmlenc#sha512"/><alg:DigestMethod Algorithm="http://www.w3.org/2001/04/xmldsig-more#sha384"/><alg:DigestMethod Algorithm="http://www.w3.org/2001/04/xmlenc#sha256"/><alg:DigestMethod Algorithm="http://www.w3.org/2001/04/xmldsig-more#sha224"/><alg:DigestMethod Algorithm="http://www.w3.org/2000/09/xmldsig#sha1"/><alg:DigestMethod Algorithm="http://www.w3.org/2001/04/xmldsig-more#rsa-sha512"/><alg:DigestMethod Algorithm="http://www.w3.org/2001/04/xmldsig-more#rsa-sha384"/><alg:DigestMethod Algorithm="http://www.w3.org/2001/04/xmldsig-more#rsa-sha256"/><alg:DigestMethod Algorithm="http://www.w3.org/2000/09/xmldsig#rsa-sha1"/><alg:DigestMethod Algorithm="http://www.w3.org/2000/09/xmldsig#dsa-sha1"/></md:Extensions><md:SPSSODescriptor AuthnRequestsSigned="true" WantAssertionsSigned="true" protocolSupportEnumeration="urn:oasis:names:tc:SAML:2.0:protocol urn:oasis:names:tc:SAML:1.0:protocol urn:oasis:names:tc:SAML:1.1:protocol"><md:Extensions xmlns:init="urn:oasis:names:tc:SAML:profiles:SSO:request-init"><init:RequestInitiator Binding="urn:oasis:names:tc:SAML:profiles:SSO:request-init" Location="http://jenkins-0.com:8080/securityRealm/finishLogin"/></md:Extensions><md:KeyDescriptor use="signing"><ds:KeyInfo xmlns:ds="http://www.w3.org/2000/09/xmldsig#"><ds:X509Data><ds:X509Certificate>MIIC+TCCAeGgAwIBAgIUARYMLNA/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7FJZdF8Ptzb33LcD5iZC9MO3Dd1/DUZ</ds:X509Certificate></ds:X509Data></ds:KeyInfo></md:KeyDescriptor><md:NameIDFormat>urn:oasis:names:tc:SAML:2.0:nameid-format:transient</md:NameIDFormat><md:NameIDFormat>urn:oasis:names:tc:SAML:2.0:nameid-format:persistent</md:NameIDFormat><md:NameIDFormat>urn:oasis:names:tc:SAML:1.1:nameid-format:emailAddress</md:NameIDFormat><md:NameIDFormat>urn:oasis:names:tc:SAML:1.1:nameid-format:unspecified</md:NameIDFormat><md:AssertionConsumerService Binding="urn:oasis:names:tc:SAML:2.0:bindings:HTTP-POST" Location="http://jenkins-0.com:8080/securityRealm/finishLogin" index="0"/></md:SPSSODescriptor></md:EntityDescriptor>
我正在做一些后期处理,即删除新行并转义"
。我将结果保存到名为POST_DATA
POST_DATA=$(cat PRE_DATA.xml | sed -e ':a' -e 'N' -e '$!ba' -e 's/\n/\\n/g' | sed 's/"/\\"/g')
现在,我想将此XML文件作为变量发送到我的--data
命令的curl
字段。
curl -X PUT \
https://my-end-point \
-H 'Authorization: Basic <TOKEN>' \
-H 'Content-Type: application/json' \
-d '{
"metadata": "$DATA",
"id": "$SP_ID"
}'
这显然不起作用,因为我没有正确发送变量。
我尝试了以下方法:
curl -X PUT \
https://my-end-point \
-H 'Authorization: Basic <TOKEN>' \
-H 'Content-Type: application/json' \
-d "{
\"metadata\": \"${DATA}\",
\"id\": \"${SP_ID}\"
}"
我也尝试过这个:
curl -X PUT \
https://my-end-point \
-H 'Authorization: Basic <TOKEN>' \
-H 'Content-Type: application/json' \
-d "$( printf '{ "metadata": "%s", "id": "%s" }' \
"${DATA}" "${SP_ID}" )"
对于以上所有情况,我都会得到:
Invalid JSON provided as input.
这些解决方案均无效。但是,当我使用Postman应用程序时,它工作得很好。基本上,我用实际值填充metadata
,它可以正常工作。我使用curl
字段中的实际值复制了metadata
命令,它可以正常工作。但是,当我将该值发送到变量并使用该变量时,它不起作用。我已经用尽了所有关于如何使这项工作的想法。有任何想法吗 ?
答案 0 :(得分:0)
箍,撤回。您已经尝试过我的建议。抱歉! (如果有人愿意在这里删除我不正确的答案,请这样做,或者告诉我如何做。)
答案 1 :(得分:0)
在JSON中转义任意文本的一种简单方法是使用jq
。会话示例:
$ DATA='<?xml version="1.0" encoding="UTF-8"?>
> <md:EntityDescriptor'
$ SP_ID=1
$ jq --arg metadata "$DATA" --arg id "$SP_ID" '{"metadata": $metadata, "id": $id}' <<< {}
{
"metadata": "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<md:EntityDescriptor",
"id": "1"
}
然后可以在jq
命令中使用curl
输出。