山pの楽しいお勉強生活

勉強の成果を垂れ流していきます

GroovyでJSONをPOSTする

概要

  • ↓をGroovyでやりたい。
curl -v -H "Accept: application/json" -H "Content-type: application/json" -X POST -d '{"id":"abcdef","user":{"name":"tarou","age":20,"email":"example@example.com","result":true}}' https://script.google.com/macros/s/xxxxxxxxx/exec
  • groovy.json.JsonBuilder を利用してJSONを作成。
  • groovyx.net.http.HTTPBuilder を利用してPOSTする。

コード

@Grab("org.codehaus.groovy.modules.http-builder:http-builder:0.7.1")

import groovyx.net.http.HTTPBuilder
import groovy.json.JsonBuilder
import static groovyx.net.http.ContentType.*

def json = new JsonBuilder()
json {
  id "abcdef"
  user {
    name "tarou"
    age 20
    email "example@example.com"
    result true
  }
}
def postBody = json.toString()

assert postBody == $/{"id":"abcdef","user":{"name":"tarou","age":20,"email":"example@example.com","result":true}}/$
/*
{
  "id":"abcdef",
  "user":{
    "name":"tarou",
    "age":20,
    "email":"example@example.com",
    "result":true
  }
}
*/

def http = new HTTPBuilder("https://script.google.com/macros/s/xxxxxxxxx/")

http.post( path: 'exec', body: postBody,
           contentType: JSON ) { resp ->
  println "POST Success: ${resp.statusLine}"
}

参考