Sample code for 30+ languages & platforms
PowerBuilder 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 PowerBuilder Downloads

PowerBuilder
integer li_rc
integer li_Success
oleobject loo_SbCurl
oleobject loo_Curl
oleobject loo_SbRawRequest

li_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>'

loo_SbCurl = create oleobject
li_rc = loo_SbCurl.ConnectToNewObject("Chilkat.StringBuilder")
if li_rc < 0 then
    destroy loo_SbCurl
    MessageBox("Error","Connecting to COM object failed")
    return
end if
loo_SbCurl.AppendLn("curl -X POST https://api.example.com/api/orders ~")
loo_SbCurl.AppendLn("  -H ~"Content-Type: application/xml; charset=utf-8~" ~")
loo_SbCurl.AppendLn("  -H ~"Accept: application/xml~" ~")
loo_SbCurl.AppendLn("  -d '<order>")
loo_SbCurl.AppendLn("  <customerName>{{customer_name}}</customerName>")
loo_SbCurl.AppendLn("  <note>{{note}}</note>")
loo_SbCurl.AppendLn("  <address>{{address}}</address>")
loo_SbCurl.AppendLn("  <instructions>{{instructions}}</instructions>")
loo_SbCurl.AppendLn("</order>'")

loo_Curl = create oleobject
li_rc = loo_Curl.ConnectToNewObject("Chilkat.HttpCurl")

//  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"
loo_Curl.SetVar("customer_name","John & Sons")
loo_Curl.SetVar("note","He said ~"Ship it ASAP!~"")
loo_Curl.SetVar("address","123 <Main> Street")
loo_Curl.SetVar("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.
loo_SbRawRequest = create oleobject
li_rc = loo_SbRawRequest.ConnectToNewObject("Chilkat.StringBuilder")

li_Success = loo_Curl.ToRawRequest(loo_SbCurl.GetAsString(),loo_SbRawRequest)
if li_Success = 0 then
    Write-Debug loo_Curl.LastErrorText
    destroy loo_SbCurl
    destroy loo_Curl
    destroy loo_SbRawRequest
    return
end if

Write-Debug loo_SbRawRequest.GetAsString()

//  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>


destroy loo_SbCurl
destroy loo_Curl
destroy loo_SbRawRequest