Different types of Web API’s ?
A Piece of software that makes itself available over the internet is called Web API .Once it is exposed to the network, other software applications can communicate and they can exchange the data.
In Simple , If an API is hosted on the network (WEB).It is called web API.
The most commonly used API on the internet are
Simple Object Access Protocol (SOAP)
Representational State Transfer (REST)
GraphQL
SOAP API:
SOAP has been in existence quite long.
It’s a protocol with a set of strict rules and standards.
Language Independent, it can work on any Internet protocol.
Examples :
Transmission Control Protocol(TCP)
User Datagram Protocol (UDP)
File Transfer Protocol(FTP)
Hypertext Transfer Protocol(HTTP)
Hypertexts Transfer Protocol Secure(HTTPS)
It can only support XML message format only.
The way soap works is by sending an envelope. Which consists of all necessary details of SOAP message ,which exchanged between the web service and the client.
Structure of the SOAP Envelope:
Example:
As per the above SOAP message ,The first of the SOAP message is the Envelope element , which is used to indicate the beginning and end of a SOAP message .Which is used to encapsulate the entire SOAP message.
SOAP body Which contains the details of the actual message.
AS per the example the name of the web service is <FullCountryInfo> .
Next comes the Parameters that are accepted by the web service.
Example of SOAP request in POSTMAN :
Steps :
Open the Postman .
Select the workspace you want to work on.
Create the collection name as CountryInfo.
Add a new request under the CountryInfo collection.
5. Copy the following link in to the "Enter URL" text box.
6.Select POST method from the drop down.
While working with Postman . We have to set the request method to POST because they have to send so much XML data.
7.While working with SOAP we have to send the XML in request body by selecting raw and from the "Text" dropdown we have to select XML. Copy the below XML envelope inside the body.
<?xml version="1.0" encoding="utf-8"?> <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"> <soap:Body> <FullCountryInfo xmlns="http://www.oorsprong.org/websamples.countryinfo"> <sCountryISOCode>String</sCountryISOCode> </FullCountryInfo> </soap:Body> </soap:Envelope>
8. Enter the "CountryISOCode" ,like IN ,US ,CA..etc
9. For this particular API ,We would also need to modify the content-Type header ,by adding new one at the bottom. We have to set the value as application/soap+xml.
10. Now click on send.
11 . We will get the full country Info back.
REST API:
REST stands for Representational State Transfer .
It is an architectural style. It doesn’t follow any strict rules like SOAP.
It mostly works on HTTP/HTTPS protocols.
It uses HTTP methods for all CRUD operations.
Create (POST)
Read (GET)
Update (PUT)
Delete (DELETE)
It is like a postcard ,which uses less bandwidth as the request doesn’t require SOAP headers for every message.
It permits different data formats such as plain text ,HTML ,XML and JSON.
To send a request we use resource URI-Endpoint
Example:
Get ListofAll
Individual data
Steps:
Open the Postman.
Create a new request.
Copy the following URL https://swapi.dev/api/people/
Select the GET method from drop down to get the list of people.
Click on send.
we will get the response with list of people .
We can authorize to an API by clicking on Authorization tab and selected the "Type" of the auth.
In below example I selected "Basic Auth " and gave Username and Password.
POST : It is used to create a record. We have to send the parameters .
we can send the parameters using query parameters or by body .
In below example I sent using body.
we will see status code as 201 Created in the response header for successful creation of the record .
PUT: We can update the existing record by using PUT method.
Here I am updating the record which I created in the POST.
Delete: Delete is used to delete the existing record .
Note: If record doesn't exists we will get an error message along with error code.
Here I deleted the record by passing the program Id as path parameter to the end point.
We will get the response header as 204 No Content.
GraphQL:
GraphQL is a query language and it was designed to deal with some of the situations where REST API’s have shortcomings.
When you call a REST API endpoint, it gives us all the information .You must call multiple endpoints to get the required data. This slow down the things in big applications with many users.
It was designed by Facebook to deal with these issues.
In GraphQL API single endpoint will contain most or all the information needed ,but we have to use queries to filter down the information
In GraphQL ,we need to know about the schema or structure of the data.
We use POST request , which will make it easier rather than using GET.
Steps:
1. Open the Postman.
2. Copy the following URL https://countries.trevorblades.com/ in the URL textbox
3. Select the POST method from the dropdown.
4. we should pass the query in the body by checking GraphQL checkbox.
If I want to know the name of the country and language then we have to give following query by passing the parameter as "US" to the code.
{ country(code: "US"){ name languages{ code name } } }
5.Click on send .
6.Got the response with name of the country ,language Name.
Hope this will help you all to get basic knowledge on working with different API’s in POSTMAN.