Skip to content
Advertisement

Getting java.io.IOException: Server returned HTTP response code: 400 for URL: when using a url which return 400 status code

I am trying to perform a get request using Groovy using the below code:

String url = “url of endpoint” def responseXml = new XmlSlurper().parse(url)

If the endpoint returns status as 200 then everything works good but there is one case where we have to validate the error response like below and status returned is 400:

JavaScript

In this case parse method throws :

JavaScript

Advertisement

Answer

In the question since we are getting 400 status code for GET request. So in built XmlSlurper().parse(URI) method does not work as it throw io.Exception. Groovy also support HTTP methods for api request and response and the below worked for me:

JavaScript
Advertisement