Last active
February 16, 2018 21:01
-
-
Save idletekz/f56b3ccd02e6f3ee416ac1c739fd1336 to your computer and use it in GitHub Desktop.
Basic Groovy URL Get/PUT/DELETE
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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