Curl POST XML Example

XML over HTTP works like JSON over HTTP with different media types, plus one extra header if the service speaks SOAP. Whitespace can matter in XML, so how you load the body matters too.

Last updated . How this page is checked.

The command

curl https://httpbin.org/post \
  -H "Content-Type: application/xml" \
  -d '<user><name>Ada</name><role>admin</role></user>'

Opens the request console with this request filled in. It is sent from api-tester.com's server rather than from your machine, so it shows what the endpoint returns, not how your own network or curl build behaves.

What the flags do

FlagWhat it does
-H "Content-Type: application/xml"Declares the request body as XML. Some older services expect text/xml.
-H "Accept: application/xml"Asks for an XML response.
--data-binary @file.xmlSend an XML file exactly as it is on disk.

Requesting XML

For a GET, the only XML-specific part is the Accept header. APIs that can produce several formats use it to choose; APIs that only produce XML ignore it. Pipe the result through xmllint to indent it.

curl -s -H "Accept: application/xml" https://httpbin.org/xml | xmllint --format -

Posting an XML file

Keep real documents in a file and send them with --data-binary. Plain -d @file deletes every newline, which is harmless for some schemas and destructive for others, such as mixed content or signed XML where the signature covers exact bytes. If the document has an XML declaration with an encoding, make the charset in the header agree with it.

curl https://api.example.com/orders \
  -H "Content-Type: application/xml; charset=utf-8" \
  -H "Accept: application/xml" \
  --data-binary @order.xml

Calling a SOAP service

SOAP is XML in an envelope, always sent by POST. SOAP 1.1 uses text/xml and requires a SOAPAction header naming the operation, quoted. SOAP 1.2 uses application/soap+xml and moves the action into a parameter of that header. Sending a 1.1 envelope with the 1.2 media type is a frequent cause of an unhelpful 500 fault.

curl https://service.example.com/StockQuote \
  -H 'Content-Type: text/xml; charset=utf-8' \
  -H 'SOAPAction: "http://example.com/GetLastTradePrice"' \
  --data-binary @envelope.xml

Common mistakes

  • A 415 usually means the service wants text/xml where you sent application/xml, or the reverse.
  • Single-quote inline XML so the shell does not interpret <, > and &.
  • SOAP faults arrive with status 500 and an XML body explaining the fault. Read the body before assuming the server crashed.

Related curl examples

More under Request bodies

References

These links leave api-tester.com, which is independent of the projects they point to.

FAQ

How do I POST XML with curl?

Send the XML with -d or --data-binary @file.xml and add -H "Content-Type: application/xml" (or text/xml if the service requires it).

How do I get an XML response with curl?

Add -H "Accept: application/xml" to the request. The server returns XML if it supports that format.

How do I send a SOAP request with curl?

POST the SOAP envelope with Content-Type: text/xml and a SOAPAction header for SOAP 1.1, or Content-Type: application/soap+xml for SOAP 1.2.