Skip to content

amiano4/httpflux

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

23 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

HttpService

HttpService is a lightweight Java HTTP client abstraction built on java.net.http.HttpClient. It simplifies sending synchronous and asynchronous HTTP requests with built-in support for headers and form-data.

Features

  • Supports GET and POST requests.
  • Handles both synchronous and asynchronous execution.
  • Allows global headers via registeredHeaders.
  • Provides callback-based success and error handling.

Usage & Installation

  1. Download the JAR file:

    • Download the .jar file here
  2. Add the JAR to Your Project:

    • For non-modular projects:

      • Include the file in your project's lib/ directory or wherever you store your JAR dependencies.
      • Add it to your classpath
    • For modular projects:

      • Simply add it to your module dependencies.
      • In your module-info.java, add the following:
        requires com.amiano4.httpflux;
      • This will make the httpflux library available to your module.

1. Set Base URL (Optional)

HttpService.setBaseUrl("https://api.example.com");

2. Send a GET Request

HttpService.get("/users")
    .onSuccess(response -> System.out.println(response.body()))
    .onError(Throwable::printStackTrace)
    .executeSync();

3. Send a POST Request with Form Data

FormDataBuilder formData = new FormDataBuilder().add("name", "John Doe");

HttpService.post("/submit", formData)
    .onSuccess(response -> System.out.println(response.body()))
    .onError(Throwable::printStackTrace)
    .executeAsync();

    // Thread.sleep(3000);

Note: Be aware of asynchronous calls. The program might end while waiting for its response.

4. Using Custom Headers

HttpHeaders headers = new HttpHeaders().add("Authorization", "Bearer token")
    .append("fileKey", "example.txt", "path/to/example.txt");

HttpService.get("/secure-data", headers)
    .onSuccess(response -> System.out.println(response.body()))
    .onError(Throwable::printStackTrace)
    .executeSync();

Available Methods

GET Requests

HttpService.get(String url);
HttpService.get(URI url);
HttpService.get(String url, HttpHeaders headers);
HttpService.get(URI url, HttpHeaders headers);

POST Requests

HttpService.post(String url);
HttpService.post(URI url);
HttpService.post(String url, FormDataBuilder formData);
HttpService.post(URI url, FormDataBuilder formData);
HttpService.post(String url, FormDataBuilder formData, HttpHeaders headers);
HttpService.post(URI url, FormDataBuilder formData, HttpHeaders headers);

License

This project is open-source. Feel free to modify and use it in your projects.

Developer Note

I've been too lazy to learn more Java features and badly wanted a straightforward approach to sending HTTP requests. So I created this library, hoping to solve the hassle.

About

Java HTTP Client with Reactive-Like Callbacks

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages