How to Invoke a REST API
Look at how to invoke a REST API.
Join the DZone community and get the full member experience.
Join For FreeProblem 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
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.
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.
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!
Opinions expressed by DZone contributors are their own.
Comments