Question

A GET request sent to https://api.github.com/users/username works from the command line and via URL#text, but fails using HTTPBuilder.

The code:

new HTTPBuilder('https://api.github.com').get(path: '/users/xan', contentType: JSON) // fails

"https://api.github.com/users/xan".toURL().text // works

On the command line:

# works:
$ curl https://api.github.com/users/xan

Also a spock test is available in this gist

Why?

Was it helpful?

Solution

Eventually I found out: GitHub denies access if the User-Agent header is missing.

This works:

def http = new HTTPBuilder('https://api.github.com')
def response = http.get(path: '/users/qmetric',
                        headers: [(USER_AGENT): "Apache HTTPClient"])

OTHER TIPS

Because you must accept the good conten type aswell.

i.e.

def http = new HTTPBuilder('http://ajax.googleapis.com')
http.request( Method.GET, ContentType.TEXT ) { req ->
  uri.path = '/ajax/services/search/web'
  uri.query = [ v:'1.0', q: 'Calvin and Hobbes' ]
  headers.Accept = 'application/json'

  response.success = { resp, reader ->
    println "Got response: ${resp.statusLine}"
    println "Content-Type: ${resp.headers.'Content-Type'}"
    print reader.text
  }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top