LoginSignup
15
15

More than 5 years have passed since last update.

RESTClientを使ってREST APIをGroovyテストする

Last updated at Posted at 2014-06-05

Webサービスをテストするのに、HTTP Builderを使ってみたら結構簡単にアクセスできた。

@Grab(group='org.codehaus.groovy.modules.http-builder', module='http-builder', version='0.7.1' )

import groovyx.net.http.RESTClient
import static groovyx.net.http.ContentType.*

// リクエストする先のURL
def http = new RESTClient('http://httpbin.org/')
// GETの場合
// パスとコンテンツタイプをMapで渡し、戻り値が結果
def res = http.get(path: 'get', contentType: JSON)
// 結果のJSONから一部表示
println res.data.origin

// POSTの場合
res = http.post(path: 'post', contentType: JSON)
println res.data.origin

参考URL

HTTP Builder - REST

15
15
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
15
15