Sample code for 30+ languages & platforms
Unicode C

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 Unicode C Downloads

Unicode C
#include <C_CkStringBuilderW.h>
#include <C_CkHttpCurlW.h>

void ChilkatSample(void)
    {
    BOOL success;
    HCkStringBuilderW sbCurl;
    HCkHttpCurlW curl;
    HCkStringBuilderW sbRawRequest;

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

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

    curl = CkHttpCurlW_Create();

    // 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"
    CkHttpCurlW_SetVar(curl,L"customer_name",L"John & Sons");
    CkHttpCurlW_SetVar(curl,L"note",L"He said \"Ship it ASAP!\"");
    CkHttpCurlW_SetVar(curl,L"address",L"123 <Main> Street");
    CkHttpCurlW_SetVar(curl,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.
    sbRawRequest = CkStringBuilderW_Create();
    success = CkHttpCurlW_ToRawRequest(curl,CkStringBuilderW_getAsString(sbCurl),sbRawRequest);
    if (success == FALSE) {
        wprintf(L"%s\n",CkHttpCurlW_lastErrorText(curl));
        CkStringBuilderW_Dispose(sbCurl);
        CkHttpCurlW_Dispose(curl);
        CkStringBuilderW_Dispose(sbRawRequest);
        return;
    }

    wprintf(L"%s\n",CkStringBuilderW_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>


    CkStringBuilderW_Dispose(sbCurl);
    CkHttpCurlW_Dispose(curl);
    CkStringBuilderW_Dispose(sbRawRequest);

    }