techjourneyman
techjourneyman

Reputation: 1813

Call external API using Mule SDK

I am trying to implement a Mule 4.x policy using Mule SDK. In doing so, I need to call an external API in the policy operations implementation. The result returned by the external API response would determine the policy output.

public class MyMulePolicyOperations
{
    @MediaType( value = ANY, strict = false )
    public void handle(
            @Config MyMulePolicyConfiguration configuration,
            @Alias( "httpRequestMethod" ) String httpRequestMethod,
            CompletionCallback<String, HttpResponse> callback ) throws Exception
    {
        HttpResponseBuilder httpResponseBuilder = HttpResponse.builder();

        String result = // call an external API and extract "result" from the response

        if ( result.equals( configuration.getMyConfigValue() ) )
        {
            httpResponseBuilder.addHeader( "allow_request", "true" );
        }
        else
        {
            httpResponseBuilder.addHeader( "allow_request", "false" );
        }

        Result<String, HttpResponse> result = Result.<String, HttpResponse> builder()
                .attributes( httpResponseBuilder.build() )
                .build();

        callback.success( result );
    }

}

Can someone tell me how I can implement the REST client using Mule SDK?

Upvotes: 0

Views: 267

Answers (1)

aled
aled

Reputation: 25664

If you want to implement an HTTP request inside a custom module, created with the Mule SDK, then you have to use the HTTP Client as described in the documentation: https://docs.mulesoft.com/mule-sdk/1.1/HTTP-based-connectors#use-the-mule-http-client

You didn't provide any reason or needs to implement the request inside a custom module. It would be way easier just to perform the HTTP request using the HTTP Requester inside the custom policy.

Upvotes: 0

Related Questions