The Hypertext Transfer Protocol (HTTP) is an application-level protocol for distributed, collaborative, hypermedia information systems. This is the foundation for data communication for the World Wide Web (i.e. internet) since 1990. HTTP is a generic and stateless protocol which can be used for other purposes as well using extensions of its request methods, error codes, and headers.
In Simple, HTTP works as a request-response protocol between a client and server.
GET requests should only be used to retrieve resource representation/information – not to modify it in any way. Because GET requests do not alter the state of the resource, they are considered safe methods.
For any given HTTP GET API, if the resource is found on the server, then it must return HTTP response code 200 (OK) – along with the response body, which is usually either XML or JSON content (due to their platform-independent nature).
A POST request is used to create a new resource into the collection of resources.
If a resource is created on the origin server, the response SHOULD be HTTP response code 201 (Created) and include an entity that describes the status of the request and refers to the new resource, as well as a Location header.
PUT APIs are primarily used to update existing resources (if the resource does not exist, then API may decide to create a new resource or not). If the PUT API creates a new resource, the origin server MUST notify the user agent via the HTTP response code 201 (Created), and if an existing resource is modified, either the 200 (OK) or 204 (No Content) response codes SHOULD be sent to indicate successful request completion.
DELETE APIs, as the name implies, are used to delete resources (identified by the Request-URI).
A successful response of DELETE requests SHOULD be HTTP response code 200 (OK) if the response includes an entity describing the status, 202 (Accepted) if the action has been queued, or 204 (No Content) if the action has been performed but the response does not include an entity.
PATCH requests are used to perform a partial update on a resource. PUT requests also modify a resource entity, so to be clear – the PATCH method is the correct choice for partially updating an existing resource, and PUT should only be used if you are completely replacing a resource.
The HTTP response codes are divided broadly in five categories :
• 1xx – Informational codes
• 2xx – Success codes
• 3xx – Redirect codes
• 4xx – Client error codes
• 5xx – Server error codes
•200 (OK): This response code indicates that the request was made successfully.
•201 (Created): This response code indicates that request was successful and a resource was created. It is used to confirm success of a PUT or POST request.
•400 (Bad Request) : This code indicates that the data is in incorrect format.
•401 (Unauthorized) : This code indicates an authentication error.
•405 (Method Not Allowed) : This code indicates that HTTP method used is not supported for this resource.
•409 (Conflict) : This code indicates that there is a conflict request to create the same resource twice.
•404 (Not Found) :This code indicates that the required resource could not be found.
•500 (Internal Server Error) : This code indicates that there is some error on Server side.
The body of your request and response message is the Payload of an API Module. It holds the information that you send to the server when you make an API request. Payload can be sent and received in a variety of formats, including JSON.
• Data is in name/value pairs
• Data is separated by commas
• Curly braces hold objects
• Square brackets hold arrays
XML stands for extensible Markup Language. It plays an important role in many different IT systemsand is often used for distributing data over the Internet.
Rest Assured is one of the most powerful libraries for testing RESTful API using Java language. This should be the first-choice when you need to test a REST. All tests should be written in the BDD (Behavior Driven Development) format and its framework syntax is very clean and easy to use. The framework sends a network request to an application under test and verifies the response based on the expectations.
- In the above example, no method has been passed to ‘given.' It means that there are no specific prerequisites.
- There is a ‘get' method that is passed to ‘when,' and it indicates that an event occurs when we visit the URL specified in the ‘get' method.
- When we hit the URL in ‘when', ‘then‘, we will receive output as a response, and then we will validate it. In this example, as shown in the above image, we are performing an assertion on the status code.