Skip to content

Configure Http Client

Santiago Gonzalez edited this page Nov 23, 2019 · 4 revisions

MSAL lets you provide your own Http client for supporting fine grained configurations.

Do use a custom Http client, you'll first need to implement IHttpClient. This class, which we will class HttpClientAdapter for the remainder of this document, should implement a method send. send should take in the HttpRequest composed by MSAL, execute it, and then construct IHttpResponse with the results of the execution.

class OkHttpClientAdapter implements IHttpClient{

    private OkHttpClient client;

    OkHttpClientAdapter(){

        // You can configure OkHttpClient 
        this.client = new OkHttpClient();  
    }

    @Override
    public IHttpResponse send(HttpRequest httpRequest) throws IOException {
        
        // Map URL, headers, and body from MSAL's HttpRequest to OkHttpClient request object  
        Request request = buildOkRequestFromMsalRequest(httpRequest);

        // Execute Http request with OkHttpClient
        Response okHttpResponse= client.newCall(request).execute();
     
        // Map status code, headers, and response body from OkHttpClient's Response object to MSAL's IHttpResponse
        return buildMsalResponseFromOkResponse(okHttpResponse);
    }

Once HttpClientAdapter has been implemented, it can be set on the client application for which you would like to use it.

        PublicClientApplication pca = PublicClientApplication.builder(
                labResponse.getAppId()).
                authority(AUTHORITY).
                httpClient( new OkHttpClientAdapter()).
                build();
Clone this wiki locally