Created
May 8, 2023 14:43
-
-
Save hugithordarson/5d5e8e1fd9128eddd3faca77b3d311ba to your computer and use it in GitHub Desktop.
This file contains hidden or 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
public static String post( final String url, final String requestBody ) throws IOException, InterruptedException { | |
// Create a client (the client can be reused so for performance sensitive code, I wouldn't do this within the method) | |
final HttpClient client = HttpClient | |
.newBuilder() | |
.build(); | |
// Construct a request to submit | |
final HttpRequest request = HttpRequest | |
.newBuilder() | |
.uri( URI.create( url ) ) | |
.POST( BodyPublishers.ofString( requestBody, StandardCharsets.UTF_8 ) ) | |
.build(); | |
// Submit the request | |
final HttpResponse<String> response = client.send( request, HttpResponse.BodyHandlers.ofString( StandardCharsets.UTF_8 ) ); | |
return response.body(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment