Skip to content

Instantly share code, notes, and snippets.

@vladfau
Last active October 31, 2024 10:14
Show Gist options
  • Save vladfau/c6b6debd5903dcbf2308162337b1a323 to your computer and use it in GitHub Desktop.
Save vladfau/c6b6debd5903dcbf2308162337b1a323 to your computer and use it in GitHub Desktop.
Sending POST + JSON Body + Auth - Libs in Java/Groovy (e.g. create tag on GitHub)
import groovy.json.JsonOutput
import java.io.BufferedWriter
import java.io.OutputStreamWriter
import java.net.URL
import java.util.Scanner
def url = new URL("https://${github}/api/v3/repos/${org}/${repo}/git/refs")
def urlConnection = url.openConnection()
urlConnection.setDoOutput(true)
urlConnection.setRequestMethod("POST")
urlConnection.setRequestProperty("Authorization", "Basic ${authString}")
urlConnection.setRequestProperty("Content-Type", "application/json")
def httpRequestBodyWriter = new BufferedWriter(new OutputStreamWriter(urlConnection.getOutputStream()))
httpRequestBodyWriter.write(JsonOutput.toJson([ref: "refs/tags/${originalVersion}", sha: completeSha]))
httpRequestBodyWriter.close()
def httpResponseScanner = new Scanner(urlConnection.getInputStream())
while(httpResponseScanner.hasNextLine()) {
println(httpResponseScanner.nextLine())
}
httpResponseScanner.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment