what is soap and rest api code example

Example 1: what is soap request

It has strict rule on how to send request and response.

Soap Request use XML format to send and receive response

Only method allowed in SOAP is POST

The content type is always xml

Body is enclosed in envelop like structure for example :

Sample Post request to turn number into word

POST https://www.dataaccess.com/webservicesserver/NumberConversion.wso

Header : text/xml; charset=utf-8

Body :

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
  <soap:Body>
    <NumberToWords xmlns="http://www.dataaccess.com/webservicesserver/">
      <ubiNum>123456789</ubiNum>
    </NumberToWords>
  </soap:Body>
</soap:Envelope>
Response

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
    <soap:Body>
        <m:NumberToWordsResponse xmlns:m="http://www.dataaccess.com/webservicesserver/">
            <m:NumberToWordsResult>one hundred and twenty three million four hundred and fifty six thousand seven hundred and eighty nine </m:NumberToWordsResult>
        </m:NumberToWordsResponse>
    </soap:Body>
</soap:Envelope>

Example 2: soap vs rest

RESTful supports JSON, XML,
	SOAP supports only XML
REST is faster than SOAP based web services
	SOAP is more secure
REST allows a greater variety of data formats,
	SOAP only allows XML.
    
    1) SOAP
- Communication happens using XML only
- Request is sent to SOAP methods
- Documentation is in WSDL (Wizdal)
- Heavy, more secure, more stable
- getting less popular
    2) REST API
- Communication happens using Json, XML
- Requests are sent to API methods/urls using
- Communication happens in HTTP(Internet)
- Lightweight, stable
- Easy to develop and automate
- very popular

Example 3: soap vs rest

RESTful:
	- is architectural style
	- stateless
	- requires HTTP
	- supports JSON, XML, HTML, CSV, plain text
	- easy documentation and easy to understand
	- efficient and faster
	- less bandwidth
	- less secure
	- Uses JAX-RS API for security

SOAP: 
	- ss XML based protocol itself
	- State or stateless
	- Can work with HTTP, SMPT(Simple Mailing Transfer Protocol), 
    FTP(File Transfer Protocol)
	- Only works with XML
	- Documentation is complex and hard to understand  
	- Slower than REST
	- More secure
	- Uses JAX-WS API for security
	- Platform and programming language independent

REST-Assured library helps us in testing our RESTful APIs easily. 
It integrates seamlessly with TestNG and JUnit. JSON Path and XML Path 
allows us to easily parse the response data and test specific elements. 
Since it uses Hamcrest matchers, there are many options to match actual 
result with expected data.

Example 4: when to use soap api

Use the SOAP API to create, retrieve,
update or delete records, like accounts
, leads, and user-defined objects.
With more than 20 different calls, 
you can also use the SOAP API to manage
passwords, perform searches, etc.
by using the SOAP API in any language 
that supports web services.

Tags:

Misc Example