2013-06-04 2 views
1

https://api.github.com/users/username으로 전송 된 GET 요청은 명령 줄에서 URL # text를 통해 작동하지만 HTTPBuilder를 사용하면 실패합니다.cURL이 잘 작동하는 동안 HTTPBuilder는 403을 얻습니다. (GitHub API)

코드 :

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

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

명령 행에서 :

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

는 또한 스팍 시험이 gist

이유에서 사용할 수 있습니까?

답변

2

은 결국 나는 발견 : 사용자 에이전트 헤더가없는 경우 GitHub의 액세스를 거부합니다.

이 작동 :

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

좋은 컨택 유형을 반드시 받아 들여야하기 때문입니다. 즉

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 
    } 
} 
+0

아니, 그것이 작동하지 않습니다 –