title: HTTP authentication in Groovy?
body: <pre><code>def http = new HTTPBuilder(url)
  http.auth.basic username, password
  
  def data = new URL(url)
  
  def response = data.getText()
  
  def relativePaths = new XmlParser().parseText(response)
  
  relativePaths.each {page -&gt;
  
      def link = page.content.@href.first()
  
      if(link.contains(".txt")){
          println link
      }
  
  }
  </code></pre>
  
  <p>Hi,</p>
  
  <p>when trying to run this, I get a 401. It seems like here:</p>
  
  <pre><code>def response = data.getText()
  </code></pre>
  
  <p>it is actually not authenticated anymore (or at all) ? In Ruby I would write it like that:</p>
  
  <pre><code>(open(url, :http_basic_authentication =&gt; [username, password])
  data = Nokogiri::XML(open(url, :http_basic_authentication =&gt; [username, password])))
  </code></pre>
  
  <p>How would that look like in Groovy ? Am I using <code>httpbuilder</code> wrong ? Thanks a lot in advance !</p>
  
  <p><strong>Update 1:</strong></p>
  
  <p>After applying the changes:</p>
  
  <pre><code>def http = new HTTPBuilder(url)
  http.auth.basic username, password
  
  http.get(path: url, contentType: ContentType.TEXT) { resp, reader -&gt;
  
      def relativePaths = new XmlParser().parse(reader)
  
      relativePaths.each { page -&gt;
  
          def link = page.content.@href.first()
  
          if (link.contains(".txt")) {
              println link
          }
      }
  }
  </code></pre>
  
  <p>I get an 406:</p>
  
  <p>Response code: 406; found handler: org.codehaus.groovy.runtime.MethodClosure@5be46f9d
  Caught: groovyx.net.http.HttpResponseException: Not Acceptable
  groovyx.net.http.HttpResponseException: Not Acceptable</p>
  
  <p><strong>Update 2:</strong></p>
  
  <p>Still not working. I changed the contentType to XML and the path to the actual path, but getting an error message:</p>
  
  <pre><code>GET &lt;URL&gt;/&lt;PATH&gt;:(id:ContinuousIntegration_BuildTestDistribute)/artifacts/children/
  Response code: 200; found handler: ArtifactDownloader$_run_closure1@5be46f9d
  Parsing response as: application/xml
  Could not find charset in response; using UTF-8
  Parsed data to instance of: class groovy.util.slurpersupport.NodeChild
  Caught: groovy.lang.MissingMethodException: No signature of method: groovy.util.XmlParser.parse() is applicable for argument types: (groovy.util.slurpersupport.NodeChild) values: []
  Possible solutions: parse(java.io.File), parse(java.io.InputStream), parse(java.io.Reader), parse(java.lang.String), parse(org.xml.sax.InputSource), use([Ljava.lang.Object;)
  groovy.lang.MissingMethodException: No signature of method: groovy.util.XmlParser.parse() is applicable for argument types: (groovy.util.slurpersupport.NodeChild) values: []
  Possible solutions: parse(java.io.File), parse(java.io.InputStream), parse(java.io.Reader), parse(java.lang.String), parse(org.xml.sax.InputSource), use([Ljava.lang.Object;)
      at ArtifactDownloader$_run_closure1.doCall(ArtifactDownloader.groovy:14)
      at groovyx.net.http.HTTPBuilder$1.handleResponse(HTTPBuilder.java:503)
      at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:218)
      at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:160)
      at groovyx.net.http.HTTPBuilder.doRequest(HTTPBuilder.java:515)
      at groovyx.net.http.HTTPBuilder.get(HTTPBuilder.java:285)
      at groovyx.net.http.HTTPBuilder$get.call(Unknown Source)
      at ArtifactDownloader.run(ArtifactDownloader.groovy:12)
      at com.intellij.rt.execution.application.AppMain.main(AppMain.java:144)
  </code></pre>
  
  <p>Just for comparison, the Ruby script. Maybe <code>httpbuilder</code> is just not what I want ? I actually wanted to use it for authentication only.</p>
  
  <pre><code>(open(url, :http_basic_authentication =&gt; [username, password])
  data = Nokogiri::XML(open(url, :http_basic_authentication =&gt; [username, password])))
  
  relativePaths = data.xpath("//content/@href")
  
  relativePaths.each { |path| 
      if path.text.include? ".apk"
          puts "&lt;URL&gt;:&lt;PORT&gt;"+path.text
      end
  }
  </code></pre>