Classic ASP Requires Chilkat v11.5.0+
Classic ASP
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 Classic ASP Downloads
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head>
<body>
<%
success = 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>'
set sbCurl = Server.CreateObject("Chilkat.StringBuilder")
success = sbCurl.AppendLn("curl -X POST https://api.example.com/api/orders \")
success = sbCurl.AppendLn(" -H ""Content-Type: application/xml; charset=utf-8"" \")
success = sbCurl.AppendLn(" -H ""Accept: application/xml"" \")
success = sbCurl.AppendLn(" -d '<order>")
success = sbCurl.AppendLn(" <customerName>{{customer_name}}</customerName>")
success = sbCurl.AppendLn(" <note>{{note}}</note>")
success = sbCurl.AppendLn(" <address>{{address}}</address>")
success = sbCurl.AppendLn(" <instructions>{{instructions}}</instructions>")
success = sbCurl.AppendLn("</order>'")
set curl = Server.CreateObject("Chilkat.HttpCurl")
' 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.
set sbRawRequest = Server.CreateObject("Chilkat.StringBuilder")
success = curl.ToRawRequest(sbCurl.GetAsString(),sbRawRequest)
If (success = 0) Then
Response.Write "<pre>" & Server.HTMLEncode( curl.LastErrorText) & "</pre>"
Response.End
End If
Response.Write "<pre>" & Server.HTMLEncode( sbRawRequest.GetAsString()) & "</pre>"
' 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>
%>
</body>
</html>