How do I use Curl to handle XML data?

How to Handle XML Data with Curl

Curl is a powerful command-line tool for transferring data to and from servers. When working with XML data, you'll typically need to send XML payloads to APIs or receive XML responses. This guide covers all aspects of XML handling with Curl.

Sending XML Data to a Server

Method 1: Sending XML from a File

When you have an XML file (data.xml), use the @ symbol to reference it:

curl -X POST \
  -H "Content-Type: application/xml" \
  -H "Accept: application/xml" \
  -d @data.xml \
  https://api.example.com/endpoint

Example XML file (data.xml):

<?xml version="1.0" encoding="UTF-8"?>
<order>
  <customer_id>12345</customer_id>
  <items>
    <item>
      <product_id>ABC123</product_id>
      <quantity>2</quantity>
    </item>
  </items>
</order>

Method 2: Sending XML as Inline String

For simple XML data, you can include it directly in the command:

curl -X POST \
  -H "Content-Type: application/xml" \
  -d '<?xml version="1.0"?><user><name>John</name><email>john@example.com</email></user>' \
  https://api.example.com/users

Method 3: Using SOAP Envelopes

For SOAP APIs, you'll need to wrap your XML in a SOAP envelope:

curl -X POST \
  -H "Content-Type: text/xml; charset=utf-8" \
  -H "SOAPAction: http://example.com/GetUserInfo" \
  -d '<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
  <soap:Body>
    <GetUserInfo xmlns="http://example.com/">
      <userId>12345</userId>
    </GetUserInfo>
  </soap:Body>
</soap:Envelope>' \
  https://api.example.com/soap

Receiving XML Data from a Server

Basic XML Retrieval

Request XML data from an API endpoint:

curl -H "Accept: application/xml" https://api.example.com/users/123

Save XML Response to File

Redirect the output to save the XML response:

curl -H "Accept: application/xml" https://api.example.com/users/123 > user_data.xml

Silent Mode with Output File

Use -s for silent mode and -o to specify output file:

curl -s -H "Accept: application/xml" -o response.xml https://api.example.com/data

Processing and Parsing XML Data

Using xmllint for Formatting

Format XML output for better readability:

curl -s https://api.example.com/xml-data | xmllint --format -

Extract Specific Elements with xmllint

Use XPath to extract specific data:

curl -s https://api.example.com/users.xml | xmllint --xpath "//user/name/text()" -

Validate XML Structure

Check if XML is well-formed:

curl -s https://api.example.com/data.xml | xmllint --noout - && echo "Valid XML"

Using xq (jq for XML)

If you have xq installed, you can query XML like JSON:

curl -s https://api.example.com/users.xml | xq '.users.user[0].name'

Advanced XML Handling Techniques

Handling XML with Authentication

Send XML data with API key authentication:

curl -X POST \
  -H "Content-Type: application/xml" \
  -H "Authorization: Bearer YOUR_API_KEY" \
  -d @request.xml \
  https://secure-api.example.com/endpoint

Custom XML Namespaces

When working with namespaced XML:

curl -X POST \
  -H "Content-Type: application/xml" \
  -d '<?xml version="1.0"?>
<ns:document xmlns:ns="http://example.com/namespace">
  <ns:title>Sample Document</ns:title>
  <ns:content>Document content here</ns:content>
</ns:document>' \
  https://api.example.com/documents

Error Handling and Debugging

Enable verbose output to debug XML requests:

curl -v -X POST \
  -H "Content-Type: application/xml" \
  -d @data.xml \
  https://api.example.com/endpoint

Check HTTP status and save both headers and body:

curl -i -X POST \
  -H "Content-Type: application/xml" \
  -d @data.xml \
  https://api.example.com/endpoint > full_response.txt

Common Content-Type Headers for XML

  • application/xml - Standard XML content type
  • text/xml - Plain text XML (older standard)
  • application/soap+xml - For SOAP 1.2 messages
  • text/xml; charset=utf-8 - XML with character encoding

Best Practices

  1. Always specify Content-Type: Use appropriate XML content type headers
  2. Handle character encoding: Specify UTF-8 encoding when needed
  3. Validate XML: Use xmllint to ensure your XML is well-formed before sending
  4. Use proper escaping: Escape special characters in inline XML strings
  5. Check response codes: Always verify the HTTP status code for successful operations
  6. Save important responses: Use output redirection to save XML responses for later analysis

Integration with Programming Languages

While Curl handles the HTTP transport, you might want to process XML programmatically:

Python example:

import subprocess
import xml.etree.ElementTree as ET

# Get XML data using curl
result = subprocess.run(['curl', '-s', 'https://api.example.com/data.xml'], capture_output=True, text=True)
root = ET.fromstring(result.stdout)

Node.js example:

const { execSync } = require('child_process');
const { DOMParser } = require('xmldom');

const xmlData = execSync('curl -s https://api.example.com/data.xml', { encoding: 'utf8' });
const doc = new DOMParser().parseFromString(xmlData, 'text/xml');

Related Questions

Get Started Now

WebScraping.AI provides rotating proxies, Chromium rendering and built-in HTML parser for web scraping
Icon