Using restassured to call API behind proxy

In most of the organisations we have proxies and while using restassured we face “java.net.ConnectException: Connection timed out” or “java.net.ConnectException: Connection refused” error as proxy does not allows the traffic.

To resolve same we need to set the proxy settings before making the call. To do the same there are two methods – either use System.setProperty or use io.restassured.specification .

While using System.setProprty again there are two ways. One is just use system proxy

System.setProperty (“java.net.useSystemProxies”, “true”);

Or set all properties manually as below

// The hostname, or address, of the proxy server used by the HTTP protocol handler.
System.setProperty("http.proxyHost", "myproxy.domain.com”);  
// The port number of the proxy server used by the HTTP protocol handler.
System.setProperty("http.proxyPort", "8080");  
// The hostname, or address, of the proxy server used by the HTTPS protocol handler.
System.setProperty("https.proxyHost", "myproxy.domain.com");  
// The port number of the proxy server used by the HTTPS protocol handler.
System.setProperty("https.proxyPort", "443”);
//Proxy User can be set 
System.setProperty("http.proxyUser", "proxyUser");
//Setting password for proxy user
System.setProperty("http.proxyPassword", "proxyPassword");

Another way is to use io.restassured.specification. Here is example

given(). proxy(host("my proxy.domain.com").withPort(8080).withAuth("proxyUser", "proxyPassword"))

One thought on “Using restassured to call API behind proxy

  1. Code of destiny says:

    I’m really impressed along with your writing talents and also with the format to your weblog. Is this a paid topic or did you modify it yourself? Anyway keep up the excellent high quality writing, it’s rare to peer a nice blog like this one today!

Leave a Reply

Your email address will not be published. Required fields are marked *