Curiosity
Curiosity

Reputation: 81

How to handle backend REST API responses in Mule Soft

I have created REST API using CRUD and I am wondering how could I handle responses from API endpoints in Anypoint Studio. There are 4 endpoints for each operation and I would like them all to share path /api in Listener module and then for Mule to choose which flow to follow depending on what comes next.

For example first flow has Listener Path set on /api, but then there is HTTP Request that has Path: http://localhost:1234/api/courses/find/60ee9678070e104b2c57be46 so it knows it is supposed to go to this flow only when I search for item with its id and not when I for example want to go to http://localhost:1234/api/courses which is for displaying all courses.

Basically what I would like to know is if I have to create 1 Listener with /api and then somehow determine what operation it is supposed to do based on passed URL or it is ok to have many flows with Listener set on /api.

Upvotes: 0

Views: 394

Answers (1)

aled
aled

Reputation: 25664

The easiest way with Anypoint Studio is to write a simple RAML specification for your API, then automatically scaffold your application with Studio.

For more information see:

If you prefer to do it more manually, without APIKit/RAML, just have the listener to the base API and use the Choice router with a condition expression using the HTTP method (attributes.method) and/or path. See https://docs.mulesoft.com/http-connector/1.6/http-listener-ref#from-http-request-to-mule-message for more attributes of the received request that you can use.

I advise the RAML method because it is easy to get started and there is tooling support.

Upvotes: 2

Related Questions