![]() |
Chilkat HOME Android™ AutoIt C C# C++ Chilkat2-Python CkPython Classic ASP DataFlex Delphi DLL Go Java JavaScript Node.js Objective-C PHP Extension Perl PowerBuilder PowerShell PureBasic Ruby SQL Server Swift Tcl Unicode C Unicode C++ VB.NET VBScript Visual Basic 6.0 Visual FoxPro Xojo Plugin
(Unicode C++) curl with Variable Substitution in an XML Request BodySee more CURL ExamplesThis 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.Note: This example requires Chilkat v11.5.0 or greater.
#include <CkStringBuilderW.h> #include <CkHttpCurlW.h> void ChilkatSample(void) { bool success = false; // 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>' CkStringBuilderW sbCurl; sbCurl.AppendLn(L"curl -X POST https://api.example.com/api/orders \\"); sbCurl.AppendLn(L" -H \"Content-Type: application/xml; charset=utf-8\" \\"); sbCurl.AppendLn(L" -H \"Accept: application/xml\" \\"); sbCurl.AppendLn(L" -d '<order>"); sbCurl.AppendLn(L" <customerName>{{customer_name}}</customerName>"); sbCurl.AppendLn(L" <note>{{note}}</note>"); sbCurl.AppendLn(L" <address>{{address}}</address>"); sbCurl.AppendLn(L" <instructions>{{instructions}}</instructions>"); sbCurl.AppendLn(L"</order>'"); CkHttpCurlW curl; // 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" curl.SetVar(L"customer_name",L"John & Sons"); curl.SetVar(L"note",L"He said \"Ship it ASAP!\""); curl.SetVar(L"address",L"123 <Main> Street"); curl.SetVar(L"instructions",L"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. CkStringBuilderW sbRawRequest; success = curl.ToRawRequest(sbCurl.getAsString(),sbRawRequest); if (success == false) { wprintf(L"%s\n",curl.lastErrorText()); return; } wprintf(L"%s\n",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 & Sons</customerName> // <note>He said "Ship it ASAP!"</note> // <address>123 <Main> Street</address> // <instructions>Use door #2 & call upon arrival</instructions> // </order> } |
||||||
© 2000-2026 Chilkat Software, Inc. All Rights Reserved.