Skip to content

Instantly share code, notes, and snippets.

@hugithordarson
Created May 8, 2023 14:43
Show Gist options
  • Save hugithordarson/5d5e8e1fd9128eddd3faca77b3d311ba to your computer and use it in GitHub Desktop.
Save hugithordarson/5d5e8e1fd9128eddd3faca77b3d311ba to your computer and use it in GitHub Desktop.
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