Skip to content

Commit a71551b

Browse files
committed
Add HttpUtility#patchJson(...)
1 parent 7334499 commit a71551b

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

src/main/java/xyz/srnyx/javautilities/HttpUtility.java

+27
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,33 @@ public static int putJson(@NotNull String userAgent, @NotNull String urlString,
128128
return responseCode;
129129
}
130130

131+
/**
132+
* Sends a PATCH request to the specified URL with the specified {@link JsonElement JSON data}
133+
*
134+
* @param userAgent the user agent to use
135+
* @param urlString the URL to send the PATCH request to
136+
* @param data the {@link JsonElement JSON data} to send with the PATCH request
137+
*
138+
* @return the response code of the request
139+
*/
140+
public static int patchJson(@NotNull String userAgent, @NotNull String urlString, @NotNull JsonElement data) {
141+
int responseCode = -1;
142+
HttpURLConnection connection = null;
143+
try {
144+
connection = (HttpURLConnection) URI.create(urlString).toURL().openConnection();
145+
connection.setRequestMethod("PATCH");
146+
connection.setRequestProperty("User-Agent", userAgent);
147+
connection.setRequestProperty("Content-Type", "application/json");
148+
connection.setDoOutput(true);
149+
connection.getOutputStream().write(data.toString().getBytes());
150+
responseCode = connection.getResponseCode();
151+
} catch (final IOException ignored) {
152+
// Ignored
153+
}
154+
if (connection != null) connection.disconnect();
155+
return responseCode;
156+
}
157+
131158
/**
132159
* Sends a DELETE request to the specified URL
133160
*

0 commit comments

Comments
 (0)