Last active
October 31, 2024 10:14
-
-
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)
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
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