How To Invoke REST Web Service in MuleSoft

This article is the continued part in the MuleSoft articles series; if you would like to understand the basics of MuleSoft, refer to these articles before going next:

What is REST WS?

REST (Representational State Transfer) is an architectural style for designing networked applications. A RESTful web service is a type of web service that adheres to the principles and constraints of REST. REST is not a protocol like SOAP (Simple Object Access Protocol) but rather a set of architectural principles that guide the design of distributed systems.

Key principles and characteristics of RESTful web services

  1. Statelessness: Each request from a client to a server must contain all the information needed to understand and process the request. The server should not store any information about the client's state between requests. This enhances scalability and simplifies the design.
  2. Client-Server Architecture: The client and server are separate entities that communicate over a network. The client is responsible for the user interface and user experience, while the server is responsible for processing requests and managing resources.
  3. Uniform Interface: A uniform and consistent way to interact with resources is provided. This includes the use of standard HTTP methods (GET, POST, PUT, DELETE) for CRUD (Create, Read, Update, Delete) operations and the use of standard resource URIs (Uniform Resource Identifiers).
  4. Resource-Based: Resources are identified by URIs, and they can represent entities such as data objects, services, or concepts. Resources are manipulated using standard HTTP methods, and their representations (often in JSON or XML format) are exchanged between the client and server.
  5. Stateless Communication: Each request from a client to a server must contain all the information needed to understand and process the request. The server should not store any information about the client's state between requests. This enhances scalability and simplifies the design.
  6. Cacheability: Responses from the server can be explicitly marked as cacheable or non-cacheable. Caching can improve performance by reducing the need for repeated requests for the same resource.
  7. Layered System: The architecture can be composed of multiple layers, with each layer providing specific functionality. This allows for flexibility and scalability.

RESTful web services are commonly used in modern web and mobile applications due to their simplicity, scalability, and ease of integration. They use standard HTTP protocols for communication, making them accessible from a variety of clients and platforms. JSON is often the preferred format for data exchange in RESTful services, but XML can also be used. Examples of RESTful web services include those provided by social media platforms, e-commerce websites, and cloud-based services.

Sample REST API

I am going to use this dummy sample REST API. https://dummy.restapiexample.com/api/v1/employees

REST API

Getting Started with Anypoint Studio

Let's do a quick project setup and include the required global config files; these are the quick steps:

First, open Anypoint Studio and Create a New Mule Project.

Create new project

Enter the project name and select runtime and project location and click the Finish button.

Project setting

Next, add a New Mule Configuration file for global settings.

Configuration file

Give the file name global and click Finish.

finish

Next, go to the global.xml file and click on the Global Elements tab and click Create button and select HTTP Listener config and click OK.

Global type

Enter the required details like listener name, protocol, host, and port. I am keeping everything as default and clicking OK.

HTTP Listener config

Also, add a new HTTP Request configuration as well.

HTTP Request configuration

Enter the host name (dummy.restapiexample.com) and click ok.

HTTP request configuration

So now we have defined two global configurations.

two global configuration

The global configuration is done for the HTTP listener and HTTP Request configuration. Let's work on Message flow. Drag and drop HTTP Listener from HTTP Palette to Message Flow and provide display name and select global connector config and provide endpoint path.

Error handling

Also, in Advanced tab provide GET in Allowed methods.

Next drag and drop Request (HTTP) from mule palettes and provide Display Name and select global configuration and give request Path. Double check the full URL in basic settings.

Basic settings

Next drag and drop Transform message component from mule palette.

Transform message

 

Update payload return type as Json and click save.

Let's run the application to see the result; right-click on the file and click Run as Mule Application. Once you see the message Deployed, everything is good, and the application is deployed.

Console

Open Postman to see the result and enter the request endpoint to see the result.

Request endpoint

Enter the endpoint and hit Send button and see the status 200 and data returned in Json format.

Conclusion

In this article, we learned how to involve REST Web Service in Mule application and how to see the result in Postman.


Similar Articles