Skip to content

Instantly share code, notes, and snippets.

@idletekz
Last active February 16, 2018 21:01
Show Gist options
  • Save idletekz/f56b3ccd02e6f3ee416ac1c739fd1336 to your computer and use it in GitHub Desktop.
Save idletekz/f56b3ccd02e6f3ee416ac1c739fd1336 to your computer and use it in GitHub Desktop.
Basic Groovy URL Get/PUT/DELETE
def authString = "username:password".getBytes().encodeBase64().toString()
def conn = addr.toURL().openConnection()
conn.setRequestProperty( "Authorization", "Basic ${authString}" )
## To perform an HTTP PUT:
URL url = new URL("http://www.example.com/resource");
HttpURLConnection httpCon = (HttpURLConnection) url.openConnection();
httpCon.setDoOutput(true);
httpCon.setRequestMethod("PUT");
OutputStreamWriter out = new OutputStreamWriter(
httpCon.getOutputStream());
out.write("Resource content");
out.close();
httpCon.getInputStream();
## To perform an HTTP DELETE:
URL url = new URL("http://www.example.com/resource");
HttpURLConnection httpCon = (HttpURLConnection) url.openConnection();
httpCon.setDoOutput(true);
httpCon.setRequestProperty(
"Content-Type", "application/x-www-form-urlencoded" );
httpCon.setRequestMethod("DELETE");
httpCon.connect();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment