forked from apiaryio/language-templates
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgroovy.html
45 lines (43 loc) · 1.57 KB
/
groovy.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
<section name="groovy" class="groovy">
<p class="ioDesc">Request</p>
<pre class="incoming"><code class="language-groovy"><% headerFirst = true %><%= "import groovyx.net.http.RESTClient\n
import static groovyx.net.http.ContentType.JSON\nimport groovy.json.JsonSlurper\nimport groovy.json.JsonOutput\n\n" %>
<% eos = "<<<EOT\n" %>
@Grab (group = 'org.codehaus.groovy.modules.http-builder', module = 'http-builder', version = '0.5.0')
def client = new RESTClient("<%= @apiUrl %>")
<% if @method is 'HEAD': %>
response = client.head( path : "<%= @url %>" )
<% end %>
<% if @method is 'GET': %>
response = client.get( path : "<%= @url %>" )
<% end %>
<% if @method is 'DELETE': %>
response = client.delete( path : "<%= @url %>" )
<% end %>
<% if @method is 'POST': %>
def jsonObj = new JsonSlurper().parseText('<%= @body %>')
response = client.post( path : "<%= @url %>",
body : jsonObj,
<% if @contentType.toLowerCase() is 'application/json': %>
contentType : JSON )
<% else: %>
contentType : ANY )
<% end %>
<% end %>
<% if @method is 'PUT': %>
def jsonObj = new JsonSlurper().parseText('<%= @body %>')
response = client.put( path : "<%= @url %>",
body : jsonObj,
<% if @contentType.toLowerCase() is 'application/json': %>
contentType : JSON )
<% else: %>
contentType : ANY )
<% end %>
<% end %>
println("Status:" + response.status)
if (response.data) {
println("Content Type: " + response.contentType)
println("Body:\n" + JsonOutput.prettyPrint(JsonOutput.toJson(response.data)))
}
</code></pre>
</section>