@Grab(group='org.apache.httpcomponents', module='httpclient', version='4.1.3') import org.apache.http.client.methods.* import org.apache.http.impl.client.* import org.apache.http.entity.* import groovy.json.* def apikey = "yourkey" def base = "https://api.mongolab.com/api/1" def db = "mydb" def col = "employee" def json = new JsonBuilder() json ( [ employeeno:"1234", name:"test1" ], [ employeeno:"5678", name:"test2" ] ) def httpclient = new DefaultHttpClient() def method = new HttpPost("${base}/databases/${db}/collections/${col}?apiKey=${apikey}") method.setHeader("Content-Type", "application/json") method.setEntity(new StringEntity(json.toString())) response = httpclient.execute(method) println response.getStatusLine().getStatusCode() println response.getEntity().getContent().text
動作環境
groovy 1.8.6
関連情報
groovyとMongoDBのまとめ
0 件のコメント:
コメントを投稿