Coding

REST vs SOAP: Differences, Examples and When to Use Each

REST vs SOAP comes down to style versus protocol. REST is an architectural style where each resource has a URL and you act on it with plain HTTP methods, while SOAP is an XML messaging protocol where every request and response is wrapped in a defined envelope and usually described by a WSDL contract.

Most new web APIs are REST, because it is lighter and works with ordinary HTTP tools. SOAP remains where a contract, formal error structure or message-level security is required, or where an existing partner system already speaks it. The sections below show the same call in both styles, then compare them.

The same request in REST and SOAP

Suppose you want to fetch a reservation. A REST API gives the reservation its own URL and uses the HTTP method to say what you want:

GET /reservations/FT35ZBQ HTTP/1.1
Host: api.example.com
Accept: application/json

Example response (the format is the server's choice, JSON here):

HTTP/1.1 200 OK
Content-Type: application/json

{ "code": "FT35ZBQ", "status": "confirmed" }

A SOAP service usually exposes one endpoint and puts the operation name inside the message. This is an example request in SOAP 1.2 format, with a made-up operation and namespace:

POST /Reservations HTTP/1.1
Host: api.example.com
Content-Type: application/soap+xml; charset="utf-8"

<?xml version="1.0"?>
<env:Envelope xmlns:env="http://www.w3.org/2003/05/soap-envelope">
  <env:Body>
    <m:getReservation xmlns:m="http://api.example.com/reservations">
      <m:code>FT35ZBQ</m:code>
    </m:getReservation>
  </env:Body>
</env:Envelope>

The W3C SOAP 1.2 primer shows the same pattern in its own travel-reservation example: a POST with Content-Type: application/soap+xml and an env:Envelope element in the namespace http://www.w3.org/2003/05/soap-envelope. The primer states that the HTTP binding for the request-response pattern "is restricted to the HTTP POST method", so even a read-only lookup is a POST.

What REST and SOAP actually are

REST comes from Roy Fielding's doctoral dissertation. It is a set of constraints, not a specification: client-server separation, stateless communication, cacheable responses, a uniform interface, a layered system, and optional code on demand. Fielding defines a resource as "a conceptual mapping to a set of entities" and a representation as "a sequence of bytes, plus representation metadata to describe those bytes". That is why REST says nothing about JSON or XML. The format is whatever representation the server sends, labelled by a media type.

SOAP is a specification. SOAP 1.2 describes itself as "a lightweight protocol intended for exchanging structured information". A message is an Envelope, which contains an optional Header and a mandatory Body. The Header holds blocks of control information (for example security or transaction data) that can be aimed at intermediaries along the path, and the Body holds the payload for the final receiver. The version 1.1 note from 2000 and the 1.2 recommendation both exist in the wild, so check which one a service uses: 1.1 sends text/xml with a SOAPAction header, while 1.2 uses application/soap+xml.

REST vs SOAP at a glance

RESTSOAP
What it isArchitectural style (constraints)Messaging protocol (W3C specification)
Message formatAny media type; JSON is commonXML envelope, always
AddressingOne URL per resourceUsually one endpoint; the operation is in the body
HTTP methodsGET, POST, PUT, PATCH, DELETE carry the meaningMostly POST
ContractOptional (OpenAPI is common)WSDL, an XML description of the service
ErrorsHTTP status codes plus a bodyA Fault element in the body
TransportHTTPTransport-independent; HTTP is the common binding
CachingHTTP caching works for GETNot by default, because requests are POST
SecurityTLS plus tokens in headersTLS, plus WS-Security for message-level protection

Contracts: WSDL vs an optional description

A SOAP service is normally described by a WSDL file. WSDL 2.0, a W3C Recommendation of 26 June 2007, is "an XML language for describing Web services". From a WSDL file, tools can generate client code with the operations and types already defined, so a client developer rarely writes XML by hand. That strict contract is the main reason enterprises kept SOAP.

REST has no required contract. Many REST APIs publish an OpenAPI document, and some publish nothing but prose documentation. That is more flexible and also easier to get wrong, because the contract lives in documentation rather than in something a tool can enforce.

Errors: Fault vs status codes

REST uses HTTP status codes, so a missing reservation is a 404 and bad input is a 400 or 422. See HTTP status codes explained for what each one means.

SOAP defines its own error structure. A Fault element in the body must contain a Code and a Reason, and the Reason is "intended to provide a human-readable explanation of the fault". Example fault, with a made-up message:

<env:Envelope xmlns:env="http://www.w3.org/2003/05/soap-envelope">
  <env:Body>
    <env:Fault>
      <env:Code><env:Value>env:Sender</env:Value></env:Code>
      <env:Reason><env:Text xml:lang="en">Reservation not found</env:Text></env:Reason>
    </env:Fault>
  </env:Body>
</env:Envelope>

Over HTTP the SOAP 1.1 note says the server "MUST issue an HTTP 500 'Internal Server Error' response" when it returns a fault. The 1.2 HTTP binding warns that proxies that rewrite error bodies "may interfere with the use of 4xx and 5xx status code responses carrying SOAP fault messages". In practice, a SOAP client reads the Fault element, not only the status code.

Security and reliability extras

With REST you normally rely on HTTPS and put a token in the Authorization header. That protects the connection between two hops. If a message passes through intermediaries, each hop ends the TLS session and can see the content.

SOAP can protect the message itself. OASIS WS-Security describes "enhancements to SOAP messaging to provide message integrity and confidentiality" and a "general-purpose mechanism for associating security tokens with message content". Because the protection is inside the Header and Body, it can survive several hops. Other WS-* specifications exist beyond security, and each has its own specification, so read the one you plan to depend on.

Performance and tooling

A SOAP envelope adds XML wrapping to every message, so payloads are larger than an equivalent compact JSON body. REST responses also gain from HTTP caching: a GET can be cached by a browser or CDN, while a SOAP POST normally cannot. Neither point means REST is always faster, because a poorly designed REST API can still need many round trips. Measure with your own payloads if the difference matters.

On tooling, REST works with curl, a browser and any HTTP library. SOAP needs an XML-aware client or generated code from the WSDL. That generated code is a convenience when it works, and a source of friction when the WSDL and client toolkit disagree.

When to use REST or SOAP

Choose REST when

  • You are building a public or mobile-facing API and want clients to start with curl or a browser.
  • Responses are cacheable, or you want smaller payloads.
  • Your resources map naturally to URLs and the standard HTTP methods.

Choose SOAP when

  • A partner, bank, government system or legacy platform already publishes a SOAP service. In that case the choice is made for you.
  • You need a formal, machine-checkable contract and generated clients across languages.
  • You need message-level security or other WS-* features, rather than transport-level protection alone.
  • The message must travel over something other than HTTP.

If you are consuming a SOAP service from a modern app, a common approach is a thin REST wrapper around it, so the rest of your system never touches XML envelopes.

Calling a SOAP service with curl

You can test a SOAP 1.2 endpoint without special tools. Save the request envelope as request.xml, then send it:

curl -X POST https://api.example.com/Reservations \
  -H "Content-Type: application/soap+xml; charset=utf-8" \
  --data-binary @request.xml

For a SOAP 1.1 service, use Content-Type: text/xml and add the SOAPAction header the service documentation specifies. The 1.1 note says this header "can be used to indicate the intent of the SOAP HTTP request". Use the real endpoint and namespaces from the service's WSDL; the ones above are placeholders.

Frequently asked questions

Is SOAP outdated?
SOAP is older and less common for new public web APIs, where REST over HTTP with JSON is the usual choice. It is still in use where an existing system, a partner or a standard already requires it, and its WS-* extensions such as WS-Security solve problems that plain REST leaves to you. Whether it is the right choice depends on the integration, not on its age.
Is REST a protocol like SOAP?
No. REST is an architectural style described by Roy Fielding, a set of constraints such as client-server, stateless communication and a uniform interface. SOAP is a protocol with a specified message format. An API can be called RESTful when it follows the constraints, and there is no standard that certifies it.
Can SOAP use JSON?
No. A SOAP message is defined as an XML envelope, so the message itself is XML. A service can carry JSON inside a text element, but then clients must parse it separately, and you give up most of what SOAP tooling offers.
Does SOAP only work over HTTP?
No. The SOAP 1.2 messaging framework is separate from the protocol that carries it, and it describes bindings such as carrying a message in an HTTP body or over a TCP stream. In practice HTTP is by far the most common binding.

Sources