PureBasic
PureBasic
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 PureBasic Downloads
IncludeFile "CkStringBuilder.pb"
IncludeFile "CkHttpCurl.pb"
Procedure ChilkatExample()
success.i = 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>'
sbCurl.i = CkStringBuilder::ckCreate()
If sbCurl.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
CkStringBuilder::ckAppendLn(sbCurl,"curl -X POST https://api.example.com/api/orders \")
CkStringBuilder::ckAppendLn(sbCurl," -H " + Chr(34) + "Content-Type: application/xml; charset=utf-8" + Chr(34) + " \")
CkStringBuilder::ckAppendLn(sbCurl," -H " + Chr(34) + "Accept: application/xml" + Chr(34) + " \")
CkStringBuilder::ckAppendLn(sbCurl," -d '<order>")
CkStringBuilder::ckAppendLn(sbCurl," <customerName>{{customer_name}}</customerName>")
CkStringBuilder::ckAppendLn(sbCurl," <note>{{note}}</note>")
CkStringBuilder::ckAppendLn(sbCurl," <address>{{address}}</address>")
CkStringBuilder::ckAppendLn(sbCurl," <instructions>{{instructions}}</instructions>")
CkStringBuilder::ckAppendLn(sbCurl,"</order>'")
curl.i = CkHttpCurl::ckCreate()
If curl.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
; 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::ckSetVar(curl,"customer_name","John & Sons")
CkHttpCurl::ckSetVar(curl,"note","He said " + Chr(34) + "Ship it ASAP!" + Chr(34))
CkHttpCurl::ckSetVar(curl,"address","123 <Main> Street")
CkHttpCurl::ckSetVar(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.
sbRawRequest.i = CkStringBuilder::ckCreate()
If sbRawRequest.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
success = CkHttpCurl::ckToRawRequest(curl,CkStringBuilder::ckGetAsString(sbCurl),sbRawRequest)
If success = 0
Debug CkHttpCurl::ckLastErrorText(curl)
CkStringBuilder::ckDispose(sbCurl)
CkHttpCurl::ckDispose(curl)
CkStringBuilder::ckDispose(sbRawRequest)
ProcedureReturn
EndIf
Debug CkStringBuilder::ckGetAsString(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 & Sons</customerName>
; <note>He said "Ship it ASAP!"</note>
; <address>123 <Main> Street</address>
; <instructions>Use door #2 & call upon arrival</instructions>
; </order>
CkStringBuilder::ckDispose(sbCurl)
CkHttpCurl::ckDispose(curl)
CkStringBuilder::ckDispose(sbRawRequest)
ProcedureReturn
EndProcedure