JAI HTTP or Just Another Implementation of HTTP is a simple library coded in Java, that consists of a server with routing and an utility for easily fetching remote resources.
It currently supports HTTP 1.0 and 1.1.
Supports loading from files, to test it clone the repository and run mvn clean install
. Test using JUnit 5.
HttpServer is created with the static function create:
HttpServer server = HttpServer.create(new InetSocketAddress("localhost", 8080));
After creating the server, you would need to add routes to it. Although it is possible, it is recommended to add all the routes before starting the server.
Routing is done by the Router, retrieved by the getRouter() method of the server.
Router router = server.getRouter();
You can use the route method to add a route to the router:
router.route("/",(req, res) ->{
res.text("Hello, World!");
});
The concept of static routing is what you would expect, it is a route that is always executed when the path matches the request.
router.staticRoute("/hello",(req, res) ->{
res.text("Hello, World!");
});
Dynamic routing is a route that can have parameters in the path, for example:
router.route("/hello/[name]",(req, res) ->{
res.headers().set("Content-Type","text/plain");
assert req.parameters() !=null;
res.body().setBody("Hello, "+req.parameters().getString("name") +"!");
});
The server can be started or stopped with the start() and stop() methods.
server.start();
server.stop();
Similarly to JavaScript's fetch, SimpleFetch is a utility for fetching remote resources.
First (but optionally) import statically the fetch method:
import static me.panjohnny.http.fetch.SimpleFetch.fetch;
Then you can use the fetch method to fetch resources:
Response apiRes = fetch("https://cataas.com/cat", null).get(); // Second parameter is optional
// Same as:
Response apiRes = fetch("https://cataas.com/cat", new RequestData(RequestMethod.GET, new Headers(), new RequestBody())).get();