Web Services Explained
Web Service can be defined as
1. Method of communication between two devices over network
2. Is a collection of standards or protocols for exchanging information between two devices or application.
3. Web service is a language independent way of communication.
For example, java application can interact with Java, .Net and PHP applications.
Types of Web Services
There are mainly two types of web services.
1. SOAP web services.
2. RESTful web services.
SOAP Web Services
SOAP stands for Simple Object Access Protocol. It is a XML-based protocol for accessing web services. SOAP is a W3C recommendation for communication between two applications.It is platform independent and language independent. By using SOAP, you will be able to interact with other programming language applications.
Advantages of Soap Web Services
WS Security: SOAP defines its own security known as WS Security.
Language and Platform independent: SOAP web services can be written in any programming language and executed in any platform.
Disadvantages of Soap Web Services
Slow: SOAP uses XML format that must be parsed to be read. It defines many standards that must be followed while developing the SOAP applications. So it is slow and consumes more bandwidth and resource.
WSDL dependent: SOAP uses WSDL and doesn't have any other mechanism to discover the service.
RESTful Web Services
Representational State Transfer (REST) is an architectural style for designing loosely coupled web services. It is mainly used to develop lightweight, fast, scalable, and easy to maintain, web services that often use HTTP as the means of communication.
OR
A REST API defines a set of operations where developers can perform requests and receive responses via HTTP protocol
Advantages of RESTful Web Services :
- Because REST API’s use HTTP, they can be used by practically any programming language
- It acts as medium to propagate communication between the client and server applications on the World Wide Web
- Stateless – No client data is stored on the server between requests and session state is stored on the client.
- http is the transport protocol for REST
Fast: RESTful Web Services are fast because there is no strict specification like SOAP. It consumes less bandwidth and resource.
Language and Platform independent: RESTful web services can be written in any programming language and executed in any platform.
Can use SOAP: RESTful web services can use SOAP web services as the implementation.
Permits different data format: RESTful web service permits different data format such as Plain Text, HTML, XML and JSON.
Rest API Examples :
The Twitter REST API
Facebook REST API (deprecating)
The key principles of REST are as follows:
• Represent everything with a unique ID; a URI
• Stateless communication
• Make use of standard HTTP methods such as GET, POST, DELETE, and PUT
GET- The GET method is used to extract information from the given server using a given URI. While using GET request, it should only extract data and should have no other effect on the data.
No Payload/Body required
POST- A POST request is used to send data to the server, for example, customer information, file upload, etc. using HTML forms.
PUT- Replaces all current representations of the target resource with the uploaded content.
DELETE- Removes all current representations of the target resource given by a URI.
• Resources can have multiple representations
REST API Automation Testing using Java
Rest-Assured is a Java based library that is used to test RESTful Web Services. This library behaves like a headless Client to access REST web services.
Rest-Assured library also provides ability to validate the HTTP Responses received from server. For e.g. we can verify the Status code, Status message, Headers and even the Body of the response.
Authentication, Part 1
There are several techniques APIs use to athenticate a client. These are called authentication schemes.
Basic Authentication
Also referred as Basic Auth. Basic Auth only requires a user name and password. The client takes these two credentials, converts them to a single value and passes that along in the HTTP header called Authentication.
The server compares the Authorization header and compares it to the credential it has stored. If it matches, the server fulfills the request. If there is no match, the server returns status code 401.
API Key Authentication
API Key Authentication is a technique that overcomes the weakness of using shared credentials. by requiring the API to be accessed with a unique key. Unlike Basic Auth, API keys were conceived at multiple companies in the early days of the web. As a result, API Key Authentication has no standard and everybody has its own way of doing it.
The most common approach has been to include it onto the URL(http://example.com?apikey=mysecret_key).
Authentication, Part 2
Open Authorization (OAuth) automates the key exchange. OAuth only requires user credentials, then behind the scenes, the client and server are chatting back and forth to get the client a valid key.
Currently there are two versions of OAuth, OAuth1 and OAuth2.
OAuth2
The players involved are:
The User - A person that wants to connect to the website The Client - The website that will be grated the access to the user's data The Server - The website that has the user's data
Step 1 — User Tells Client to Connect to Server
Step 2 — Client Directs User to Server
The client sends the user over to the server’s website, along with a URL that the server will send the user back to once the user authenticates, called the callback URL.
Step 3 — User Logs-in to Server and Grants Client Access
With their normal user name and password, the user authenticates with the server.
Step 4 — Server Sends User Back to Client, Along with Code
The server sends the user back to the client using the callback URL. Hidden in the response is a unique authorization code for the client.
Step 5 — Client Exchange Code + Secret Key for Access Token
The client takes the authorization code it receives and makes another request to the server. This request includes the client’s secret key. When the server sees a valid authorization code and a trusted client secret key, it is certain that the client is who it claims. The server responds back with an access token.
Step 6 — Client Fetches Data from Server.
The access token from Step 5 is essentially another password into the user’s account on the server. The client includes the access token with every request so it can authenticate directly with the server.
Client Refresh Token (Optional)
A feature in OAuth 2 is the option to have access tokens expire. The lifespan of a token is set by the server.
Authorization
Authorization is the concept of limiting access. In Step 2, when the user allows the client access, buried in the fine print are the exact permissions the client is asking for. Those permission are called scope.
What makes scope powerful is that is client-based restrictions. OAuth scope allows one client to have permission X and another to have permission X and Y.
Comments
Post a Comment