@Grab(group='org.apache.httpcomponents', module='httpclient', version='4.1.2') import org.apache.http.* import org.apache.http.client.* import org.apache.http.client.entity.* import org.apache.http.client.methods.* import org.apache.http.impl.client.* import org.apache.http.message.* import org.apache.http.protocol.* import groovy.json.* // ログイン httpclient = new DefaultHttpClient() method = new HttpPost("http://localhost/drupal/rest/user/login") values = [ new BasicNameValuePair("username", "drupal"), new BasicNameValuePair("password", "drupal") ] method.setEntity(new UrlEncodedFormEntity(values, HTTP.UTF_8)) response = httpclient.execute(method) println response.getStatusLine().getStatusCode() println response.getEntity().getContent().text // ユーザを登録する method = new HttpPost("http://localhost/drupal/rest/user.json") values = [ new BasicNameValuePair('name', 'newuser'), new BasicNameValuePair('mail', 'youraccount@gmail.com'), new BasicNameValuePair('pass', 'drupal') ] method.setEntity(new UrlEncodedFormEntity(values, HTTP.UTF_8)) response = httpclient.execute(method) json = new JsonSlurper().parseText(response.getEntity().getContent().text) println json // ログアウト method = new HttpGet("http://localhost/drupal/user/logout") response = httpclient.execute(method) println response.getStatusLine().getStatusCode()
動作環境
groovy 1.8.4, JDK7 Update1, drupal 7.1.0, services-7.x-3.1, ctools-7.x-1.0-rc1, spyc-0.5
0 件のコメント:
コメントを投稿