Timeout health and 502 exceptions

Prevent HTTP timeouts and socket exceptions when calling the MailSlurp API by configuring client timeouts within your SDK client.

  • Table of contents

MailSlurp uses 'Keep-Alive' connections for many endpoints to allow your code to wait for conditions.

SDK timeout settings

It is important to set a connection timeout on the client and its underlying http client implementations when using the MailSlurp SDKs.

For example, in Java pass an OkHttpClient instance to the MailSlurpClient and set read, write and connect timeouts.


public class MailSlurpExamplesTest {

    private static ApiClient apiClient;
    private static final Long TIMEOUT = 30000L;
    private static final String apiKey = System.getenv("API_KEY");

    @BeforeAll
    public static void beforeAll() throws Exception {
        // get API KEY for mailslurp from environment variable
        if (StringUtils.isBlank(apiKey)) {
            throw new Exception("Must provide API KEY");
        }

        // IMPORTANT set timeout for the http client
        OkHttpClient httpClient = new OkHttpClient.Builder()
                .connectTimeout(TIMEOUT, TimeUnit.MILLISECONDS)
                .writeTimeout(TIMEOUT, TimeUnit.SECONDS)
                .readTimeout(TIMEOUT, TimeUnit.SECONDS)
                .build();

        apiClient = Configuration.getDefaultApiClient();

        // IMPORTANT set api client timeouts
        apiClient.setConnectTimeout(TIMEOUT.intValue());
        apiClient.setWriteTimeout(TIMEOUT.intValue());
        apiClient.setReadTimeout(TIMEOUT.intValue());

        // IMPORTANT set API KEY and client
        apiClient.setHttpClient(httpClient);
        apiClient.setApiKey(apiKey);
    }
}

This will help to avoid java.net.SocketTimeoutException errors that you may encounter with default timeout settings.

The same approach applies to other languages.

Test framework settings

Sometimes connection exceptions can occur in testing frameworks such as Junit or Jest. We recommend setting similar timeouts for your test framework. In Javascript Jest this looks like:

jest.setTimeout(TIMEOUT)

Most MailSlurp operations take several seconds. Waiting for emails is different and can take up to 120 seconds depending on the number of emails you are waiting for and the load on MailSlurp servers. Most clients take timeout values in milliseconds.

const TIMEOUT = 120000

Support for continuing exception

If your connection or 502 errors continue please contact support and provide code examples so we can debug the issue further.

Email and SMS Platform
Create a free account in 3 clicks