@Grab(group='org.apache.httpcomponents', module='httpclient', version='4.1.3') import org.apache.http.client.methods.* import org.apache.http.impl.client.* import groovy.json.* def apikey = "your-key" def base = "https://api.mongolab.com/api/1" def db = "mydb" def col = "employee" def params = "apiKey=${apikey}&q=" + URLEncoder.encode("{employeeno:\"1234\"}", "UTF-8") def httpclient = new DefaultHttpClient() def method = new HttpGet("${base}/databases/${db}/collections/${col}?${params}") response = httpclient.execute(method) println response.getStatusLine().getStatusCode() def content = new InputStreamReader( response.getEntity().getContent(), "UTF-8").text def json = new JsonSlurper().parseText(content) for(employee in json){ println employee.employeeno println employee.name }
動作環境
groovy 1.8.6
関連情報
groovyとMongoDBのまとめ
0 件のコメント:
コメントを投稿