Sample code for 30+ languages & platforms
Android™

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 Android™ Downloads

Android™
// Important: Don't forget to include the call to System.loadLibrary
// as shown at the bottom of this code sample.
package com.test;

import android.app.Activity;
import com.chilkatsoft.*;

import android.widget.TextView;
import android.os.Bundle;

public class SimpleActivity extends Activity {

  private static final String TAG = "Chilkat";

  // Called when the activity is first created.
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

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

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

    CkHttpCurl curl = new CkHttpCurl();

    // 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("customer_name","John & Sons");
    curl.SetVar("note","He said \"Ship it ASAP!\"");
    curl.SetVar("address","123 <Main> Street");
    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.
    CkStringBuilder sbRawRequest = new CkStringBuilder();
    success = curl.ToRawRequest(sbCurl.getAsString(),sbRawRequest);
    if (success == false) {
        Log.i(TAG, curl.lastErrorText());
        return;
        }

    Log.i(TAG, 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>

  }

  static {
      System.loadLibrary("chilkat");

      // Note: If the incorrect library name is passed to System.loadLibrary,
      // then you will see the following error message at application startup:
      //"The application <your-application-name> has stopped unexpectedly. Please try again."
  }
}