DataFlex
DataFlex
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 DataFlex Downloads
Use ChilkatAx-win32.pkg
Procedure Test
Boolean iSuccess
Handle hoSbCurl
Handle hoCurl
Variant vSbRawRequest
Handle hoSbRawRequest
String sTemp1
Move False To iSuccess
// 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>'
Get Create (RefClass(cComChilkatStringBuilder)) To hoSbCurl
If (Not(IsComObjectCreated(hoSbCurl))) Begin
Send CreateComObject of hoSbCurl
End
Get ComAppendLn Of hoSbCurl "curl -X POST https://api.example.com/api/orders \" To iSuccess
Get ComAppendLn Of hoSbCurl ' -H "Content-Type: application/xml; charset=utf-8" \' To iSuccess
Get ComAppendLn Of hoSbCurl ' -H "Accept: application/xml" \' To iSuccess
Get ComAppendLn Of hoSbCurl " -d '<order>" To iSuccess
Get ComAppendLn Of hoSbCurl " <customerName>{{customer_name}}</customerName>" To iSuccess
Get ComAppendLn Of hoSbCurl " <note>{{note}}</note>" To iSuccess
Get ComAppendLn Of hoSbCurl " <address>{{address}}</address>" To iSuccess
Get ComAppendLn Of hoSbCurl " <instructions>{{instructions}}</instructions>" To iSuccess
Get ComAppendLn Of hoSbCurl "</order>'" To iSuccess
Get Create (RefClass(cComChilkatHttpCurl)) To hoCurl
If (Not(IsComObjectCreated(hoCurl))) Begin
Send CreateComObject of hoCurl
End
// 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"
Send ComSetVar To hoCurl "customer_name" "John & Sons"
Send ComSetVar To hoCurl "note" 'He said "Ship it ASAP!"'
Send ComSetVar To hoCurl "address" "123 <Main> Street"
Send ComSetVar To hoCurl "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.
Get Create (RefClass(cComChilkatStringBuilder)) To hoSbRawRequest
If (Not(IsComObjectCreated(hoSbRawRequest))) Begin
Send CreateComObject of hoSbRawRequest
End
Get ComGetAsString Of hoSbCurl To sTemp1
Get pvComObject of hoSbRawRequest to vSbRawRequest
Get ComToRawRequest Of hoCurl sTemp1 vSbRawRequest To iSuccess
If (iSuccess = False) Begin
Get ComLastErrorText Of hoCurl To sTemp1
Showln sTemp1
Procedure_Return
End
Get ComGetAsString Of hoSbRawRequest To sTemp1
Showln sTemp1
// 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>
End_Procedure