Sample code for 30+ languages & platforms
Tcl Requires Chilkat v11.5.0+

curl with Variable Substitution in an XML Request Body

See more CURL Examples

This example shows how to use variables inside an XML request body using the {{variable_name}} syntax. When the HTTP request’s Content-Type indicates XML, Chilkat automatically applies proper XML entity encoding to each substituted value, ensuring the resulting XML remains valid.

Chilkat Tcl Downloads

Tcl

load ./chilkat.dll

set success 0

#  Variable names are enclosed between {{ and }}

#  curl -X POST https://api.example.com/api/orders \
#    -H "Content-Type: application/xml; charset=utf-8" \
#    -H "Accept: application/xml" \
#    -d '<order>
#    <customerName>{{customer_name}}</customerName>
#    <note>{{note}}</note>
#    <address>{{address}}</address>
#    <instructions>{{instructions}}</instructions>
#  </order>'

set sbCurl [new_CkStringBuilder]

CkStringBuilder_AppendLn $sbCurl "curl -X POST https://api.example.com/api/orders \\"
CkStringBuilder_AppendLn $sbCurl "  -H \"Content-Type: application/xml; charset=utf-8\" \\"
CkStringBuilder_AppendLn $sbCurl "  -H \"Accept: application/xml\" \\"
CkStringBuilder_AppendLn $sbCurl "  -d '<order>"
CkStringBuilder_AppendLn $sbCurl "  <customerName>{{customer_name}}</customerName>"
CkStringBuilder_AppendLn $sbCurl "  <note>{{note}}</note>"
CkStringBuilder_AppendLn $sbCurl "  <address>{{address}}</address>"
CkStringBuilder_AppendLn $sbCurl "  <instructions>{{instructions}}</instructions>"
CkStringBuilder_AppendLn $sbCurl "</order>'"

set curl [new_CkHttpCurl]

#  The values below contain chars that will require XML entity encoding.  
#  Chilkat will automatically do the encoding because the Content-Type of this request is "application/xml"
CkHttpCurl_SetVar $curl "customer_name" "John & Sons"
CkHttpCurl_SetVar $curl "note" "He said \"Ship it ASAP!\""
CkHttpCurl_SetVar $curl "address" "123 <Main> Street"
CkHttpCurl_SetVar $curl "instructions" "Use door #2 & call upon arrival"

#  To demonstrate how the variables are replaced, this example does not execute the curl command. 
#  Instead, it generates the raw HTTP request that would be sent if the curl command were run.
set sbRawRequest [new_CkStringBuilder]

set success [CkHttpCurl_ToRawRequest $curl [CkStringBuilder_getAsString $sbCurl] $sbRawRequest]
if {$success == 0} then {
    puts [CkHttpCurl_lastErrorText $curl]
    delete_CkStringBuilder $sbCurl
    delete_CkHttpCurl $curl
    delete_CkStringBuilder $sbRawRequest
    exit
}

puts [CkStringBuilder_getAsString $sbRawRequest]

#  The output is shown below.
#  Notice the chars that were XML entity encoded.

#  POST /api/orders HTTP/1.1
#  Accept: application/xml
#  Host: api.example.com
#  Content-Type: application/xml; charset=utf-8
#  Content-Length: 229
#  
#  <order>
#    <customerName>John &amp; Sons</customerName>
#    <note>He said &quot;Ship it ASAP!&quot;</note>
#    <address>123 &lt;Main&gt; Street</address>
#    <instructions>Use door #2 &amp; call upon arrival</instructions>
#  </order>

delete_CkStringBuilder $sbCurl
delete_CkHttpCurl $curl
delete_CkStringBuilder $sbRawRequest