What is the difference between REST and SOAP? Which HTTP methods are idempotent?

REST versus SOAP compared on format, contract, transport and error handling, which HTTP methods are safe and idempotent, and the status codes to know.

0 0
Read Time:1 Minute, 51 Second

REST versus SOAP

REST                              SOAP
What it is      An architectural style            A protocol with a strict spec
Format          Usually JSON (any format allowed) XML envelope only
Contract        Optional (OpenAPI)                WSDL, required
Transport       HTTP                              HTTP, SMTP, others
Errors          HTTP status codes                 SOAP Fault in the body
Extras          Relies on HTTPS, OAuth            WS-Security, WS-ReliableMessaging
Typical use     Web and mobile APIs, microservices Older enterprise, banking, telecom

REST models resources with URLs (/orders/42) and uses HTTP methods as the verbs. It is stateless: each request carries everything the server needs, such as a token.

Safe and idempotent methods

Idempotent means sending the same request once or ten times leaves the server in the same state. Safe means it does not change state at all.

Method   Safe  Idempotent  Typical use
GET      yes   yes         Read a resource
HEAD     yes   yes         Headers only
OPTIONS  yes   yes         Allowed methods, CORS preflight
PUT      no    yes         Replace a resource at a known URL
DELETE   no    yes         Remove a resource
POST     no    no          Create, or trigger an action
PATCH    no    not always  Partial update

DELETE is idempotent even though the second call may return 404: the server state (resource gone) is the same. PATCH depends on the body: “set name to X” is idempotent, “add 1 to count” is not.

Why it matters

Clients and proxies may retry idempotent requests safely after a timeout. To make POST safe to retry, for example for payments, accept an Idempotency-Key header and return the first result for repeated keys.

Status codes to know

  • 200 OK, 201 Created (with a Location header), 204 No Content.
  • 301 and 302 redirects, 304 Not Modified.
  • 400 Bad Request, 401 Unauthorized (not logged in), 403 Forbidden (logged in, not allowed), 404 Not Found, 409 Conflict, 422 validation error, 429 Too Many Requests.
  • 500 Internal Server Error, 502 Bad Gateway, 503 Service Unavailable.

Follow-ups

  • PUT versus PATCH.
  • How do you version a REST API?
  • What does stateless mean, and where does the session go?

This answer was written by Debugger’s Space as practice material. It is asked in most Indian IT services interviews (TCS, Infosys, Wipro, HCLTech, Tech Mahindra, LTIMindtree, Accenture, Cognizant, Capgemini, IBM). See each company’s hiring steps and practice questions on Debugger’s Space.

Previous post What happens when you type a URL in the browser and press Enter?
Next post What is the difference between IaaS, PaaS and SaaS?
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Close
0
Would love your thoughts, please comment.x
()
x