{{announcement.body}}
{{announcement.title}}

How to Invoke a REST API

DZone 's Guide to

How to Invoke a REST API

Look at how to invoke a REST API.

· Integration Zone ·
Free Resource

Problem Statement

Recently, while working with MuleSoft 4 (AnyPoint Platform - 7.3.1), I had come across a scenario that requires the invocation of another REST API, which is also implemented on MuleSoft. This API requires input in the form of URI parameters. This API works well when invoked from an external client like Postman, however, exceptions are observed when the same API is invoked from MuleSoft.

Implementation Details

App - Currency Conversation is implemented for providing currency conversion details in real-time, and this APP can be invoked by URL  -http://<<hostname>> /api/ currenyconversion /baseCcy /<<value>> /targetCcy/<<value>>

E.g - http://localhost:8081/api/currenyconversion/baseCcy/USD/targetCcy/INR

Observations

  1. Sucessfile response is observed when this API is invoked by clients like Postman etc., however, when this scenario is invoked from MuleSoft, i.e a MuleSoft flow that invokes this particular API, exceptions were observed.

  2. After further debugging, it was found that when this API was invoked with Postman, rawRequestPath of API is found to be of type "/api/currenyconversion /baseCcy /NZD/targetCcy/INR", which clearly describes on input parameters that require transformation. 

  3. When this API is invoked from another Mule app, the raw request Path of the API is found to be of type  "/api/currenyconversion". In this case, input parameters are found to be missing that are required for data processing.

Code Snippet of HTTP requestor configuration that invokes API

<http:request method="GET" doc:name="Request" doc:id="c0c65c9a-8263-4442-a250-6922531925c2" path="#['/currenyconversion/']" sendCorrelationId="ALWAYS" config-ref="invesco-httpRequestor-processApi"> <http:uri-params > <![CDATA[#[output application/java --- { baseCcy : vars.queryVarBaseCcy, targetCcy : vars.queryVarTargetCcy }]]]> </http:uri-params> </http:request>

Troubleshooting and Workaround

There seems to be some technical glitch due to the URI parameters not getting appended to the request URL. So, a workaround is required to append URI Parameters to the path.

<http:request method="GET" doc:name="Request" doc:id="c0c65c9a-8263-4442-a250-6922531925c2" sendCorrelationId="ALWAYS" config-ref="invesco-httpRequestor-processApi" path="#['/currenyconversion/baseCcy/' ++ vars.queryVarBaseCcy ++ '/targetCcy/'++ vars.queryVarTargetCcy]"> </http:request>

Thank you!

Topics:
mulesoft 4 ,integration ,tutorial ,invoke rest api

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}