Skip to content
Tutorials Center
My learning

Topics

Lesson 19 of 36 ยท REST vs SOAP

SOAP vs REST, Side by Side

Compare SOAP and REST by implementing one operation both ways in Node.js - messages, URLs, verbs, errors, caching and contracts - and see what each style really costs you.

SOAP and REST are often described as rivals, but they are better understood as answers to different questions. SOAP asks, "How do two programs agree on an operation, its inputs and its failures, even when they are built by different companies and talk over different transports?" REST asks, "How can we expose resources over plain HTTP so that the web's own machinery (URLs, caching, status codes) does most of the work?"

The fastest way to feel the difference is to build the same small feature both ways. In this lesson you will implement "look up the price of a product" as a SOAP operation and as a REST resource, inside one Node.js program, and then compare the traffic that crosses the wire.

Everything below runs on our Node.js runner using only the standard library. The server listens on loopback and the program calls itself with fetch. There is no SOAP toolkit here: the SOAP messages are plain XML strings that we build and read ourselves, which is the best way to see what a toolkit does for you.

The same feature, two shapes

Imagine a catalog with three products. Both services answer one question: what does product P-100 cost?

Aspect SOAP REST
Endpoint One URL, e.g. /catalog Many URLs, e.g. /products/P-100
HTTP method Always POST GET for reads
Operation name Inside the XML body (GetPrice) Implied by method + URL
Payload XML envelope JSON (or any media type)
Errors Fault element in the body HTTP status code + error body
Contract WSDL (machine-readable) OpenAPI (optional, separate)

Notice that SOAP treats HTTP as a pipe that carries messages, while REST treats HTTP as the application protocol itself. That one choice explains most of the other differences.

SOAP in one slide

Envelope Every call is an XML message
WSDL contract Operations and types are described
One endpoint POST everything to one URL

The operation name travels inside the message.

1 / 2

Building both services

The next program starts one HTTP server that serves both styles. Read the two handlers side by side: the logic (a lookup in a Map) is identical, only the packaging differs.

Node.js One lookup, two styles

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

Look at the output. The REST lookup for a missing product returns 404, and any HTTP-aware tool (a browser, a proxy, a monitoring system) understands that immediately. The SOAP fault also returns an HTTP error status, but the meaning (soap:Client, "Unknown product") lives in the XML body, so you need a SOAP-aware client to interpret it.

What travels on the wire

Compare the sizes of the two exchanges for the same answer. Counting bytes shows the cost of an envelope and namespaces versus a tiny JSON object.

Node.js Measuring the envelope tax

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

The envelope is not wasted effort: it is where headers for security, routing and transactions live (the WS-Security chapter covers them). But for a plain lookup, it is overhead, and that overhead is the price of the extra capabilities.

Where each style wins

SOAP is strong when you need a formal contract, standardized security and reliability extensions, or a transport other than HTTP. REST is strong when you want cacheable reads, simple clients (a browser is enough), and loose coupling that lets URLs and representations evolve.

A few concrete differences to remember:

  • Caching. A GET /products/P-100 can be cached by browsers and CDNs. A SOAP POST cannot, by the rules of HTTP, so every call reaches your server.
  • Tooling. SOAP clients are usually generated from a WSDL, giving typed methods. REST clients are typically hand-written or generated from OpenAPI.
  • Coupling. SOAP clients are tightly bound to the operation names and schema. REST clients bind to URLs and media types, and good REST uses hypermedia to loosen even that.
  • Errors. SOAP defines one fault structure; REST relies on status codes plus whatever error body the API chooses (often Problem Details).

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Which HTTP method does a SOAP 1.1 call normally use?

    SOAP messages are carried in the HTTP body, so the call is a POST to a single endpoint. The operation name is inside the XML, not in the verb.
  2. 2. Why can CDNs cache a REST GET but not a SOAP call?

    Caches key on URL and method. Safe GETs to distinct URLs are cacheable; POSTs to one endpoint are not.
  3. 3. Where does a SOAP service put the description of an error?

    SOAP defines a Fault element in the Body with a code and a message, and the HTTP status (500 for SOAP 1.1) is just a hint.
  4. 4. Which statement is the best summary of the difference?

    That design choice drives the differences in endpoints, verbs, errors and caching.

Summary

  • SOAP uses one endpoint, always POST, with the operation inside an XML envelope; REST uses many URLs and the HTTP verb.
  • SOAP errors are Fault elements; REST errors are status codes plus a body.
  • A SOAP message has envelope overhead, but that envelope carries security and routing headers.
  • REST reads are cacheable; SOAP calls are not.
  • SOAP offers a formal contract (WSDL); REST contracts are optional (OpenAPI).

Next: Choosing Between SOAP, REST, gRPC and GraphQL