Skip to content

Commit

Permalink
Add HttpUtility#patchJson(...)
Browse files Browse the repository at this point in the history
  • Loading branch information
srnyx committed May 25, 2024
1 parent 7334499 commit a71551b
Showing 1 changed file with 27 additions and 0 deletions.
27 changes: 27 additions & 0 deletions src/main/java/xyz/srnyx/javautilities/HttpUtility.java
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,33 @@ public static int putJson(@NotNull String userAgent, @NotNull String urlString,
return responseCode;
}

/**
* Sends a PATCH request to the specified URL with the specified {@link JsonElement JSON data}
*
* @param userAgent the user agent to use
* @param urlString the URL to send the PATCH request to
* @param data the {@link JsonElement JSON data} to send with the PATCH request
*
* @return the response code of the request
*/
public static int patchJson(@NotNull String userAgent, @NotNull String urlString, @NotNull JsonElement data) {
int responseCode = -1;
HttpURLConnection connection = null;
try {
connection = (HttpURLConnection) URI.create(urlString).toURL().openConnection();
connection.setRequestMethod("PATCH");
connection.setRequestProperty("User-Agent", userAgent);
connection.setRequestProperty("Content-Type", "application/json");
connection.setDoOutput(true);
connection.getOutputStream().write(data.toString().getBytes());
responseCode = connection.getResponseCode();
} catch (final IOException ignored) {
// Ignored
}
if (connection != null) connection.disconnect();
return responseCode;
}

/**
* Sends a DELETE request to the specified URL
*
Expand Down

0 comments on commit a71551b

Please sign in to comment.