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
| Flag | What 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.xml | Send 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
415usually means the service wantstext/xmlwhere you sentapplication/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
Curl GET XML Example
Fetching XML is a GET with an Accept header.
Curl POST JSON Example
Sending JSON takes two things: the JSON itself and a header saying that is what it is.
Curl Content-Type Example
Content-Type tells the server how to parse the body you sent.
Curl POST File Example
There are three different things people mean by "post a file", and they are three different requests.
All Curl Examples
49 worked examples, grouped by what you are trying to do.
More under Request bodies
References
- curl man page: --data-binary — Sending a file exactly as it is on disk.
- MDN: MIME types — application/xml, text/xml and how media types are structured.
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.