Do not follow redirects when using RequestObject and ResponseObject

I’m testing our website’s 301 redirects and need a way to get the status code of a ResponseObject without it actually following the redirect itself (i.e. I’m expecting a status code of 301 instead of 200).

Here is a sample of my script:

import com.kms.katalon.core.testobject.RequestObject
import com.kms.katalon.core.testobject.ResponseObject
import com.kms.katalon.core.webservice.keyword.WSBuiltInKeywords as WSBuiltInKeywords
import com.kms.katalon.core.logging.KeywordLogger

String url = 'http://google.com'
String urlResponse

RequestObject ro = new RequestObject()
ResponseObject resp = new ResponseObject()
KeywordLogger log = new KeywordLogger()

ro.setRestRequestMethod("GET")
ro.setRestUrl(url)
resp = WSBuiltInKeywords.sendRequest(ro)
urlResponse = resp.getStatusCode()

log.logInfo(urlResponse)

Of course you’d expect google.com to return 200, but pretend the URL has a 301 redirect set up to forward the user to a different page – in this scenario, Katalon will actually follow the redirect and return the status code based on the destination page, rather than returning a 301 status code from the source page.

Postman allows this by turning off the ‘automatically follow redirect’ setting. I’m wondering if there’s a particular method I can call on the RequestObject or ResponseObject in order to achieve this in Katalon too.

2 Likes

It is a nice suggestion.

I am also facing a similar issue. In my case, it is redirecting to another URL and its response code is 500. This redirect will also update the cookie value set in the initial request. Below is the code which i tried

TestObjectProperty header = new TestObjectProperty("Content-Type", ConditionType.EQUALS, "application/x-www-form-urlencoded")


ArrayList defaultHeaders = Arrays.asList(header)

List<UrlEncodedBodyParameter> urlparam = new ArrayList<UrlEncodedBodyParameter>();
urlparam.add(new UrlEncodedBodyParameter("username", "xxxxx"))
urlparam.add(new UrlEncodedBodyParameter("password", "xxxxx"))


RequestObject requestObject = new RequestObject("RequestObject")
requestObject.setFollowRedirects(false)
requestObject.setRestUrl("https://URL_To_APP")
requestObject.setHttpHeaderProperties(defaultHeaders)
requestObject.setRestRequestMethod("POST")
requestObject.setBodyContent(new HttpUrlEncodedBodyContent(urlparam))

 
ResponseObject respObjectAfterApiCall = WS.sendRequest(requestObject)

println respObjectAfterApiCall.getHeaderFields()['Set-Cookie']

assert respObjectAfterApiCall.getStatusCode() == 200

Is there anyway to stop redirecting from happening. I am not seeing any behavior like that in Postman or chrome browser network tab.

1 Like

@Brian_Ducson

It will be great if this feature is implemented. As of now i don’t have way to turn off redirection. Thanks for all the great work.

Hi guys.

Could you use the latest version, there’s a way to configurate this.

Cheers !

Could you use the latest version, there’s a way to configurate this.

Awesome, I just tried it in 6.2.1 and it works great. Thank you!

1 Like