0

What's the easiest way to post JSON to a URL in groovy using only the standard libraries? (ie, without using HttpClient or HttpClientBuilder)

This is a script I'd rather not have to link in lots of other libraries to.

Brad Lee
  • 629
  • 7
  • 11

1 Answers1

1
def con = "http://endpointUrl".toURL().openConnection();
con.setDoOutput(true);
con.setDoInput(true);
con.setRequestProperty("Content-Type", "application/json");
con.outputStream.withWriter { writer ->
  writer << jsonString
}

String response = con.inputStream.withReader { Reader reader -> reader.text }
Brad Lee
  • 629
  • 7
  • 11
  • Note that if you are using HTTPS I found that you will likely need this code before hand: System.setProperty("jsse.enableSNIExtension", "false"); // @see http://stackoverflow.com/questions/7615645/ssl-handshake-alert-unrecognized-name-error-since-upgrade-to-java-1-7-0 – Brad Lee Dec 24 '15 at 16:30