This is a simple HTTP server implemented in Java using non-blocking I/O with the java.nio
package. The server supports handling GET, POST, PUT, PATCH, and DELETE requests.
- Handles GET, POST, PUT, PATCH, and DELETE requests
- Supports JSON and multipart/form-data request bodies
- Allows registering custom handlers for specific paths and HTTP methods
To run the HTTP server, follow these steps:
-
Clone the repository:
git clone https://github.com/your-username/http-server.git
-
Navigate to the project directory:
cd httpServer
-
Compile the Java source files:
mvn clean package
-
Run the server:
java -jar target/httpServer-1.0-SNAPSHOT.jar
The server will start running on localhost
with the port 8081.
You can use Postman to send requests to the server. Here some examples:
Send a GET request to the server:
Send a POST request with a JSON body:
Send a PUT request with multipart/form-data containing form fields and files:
Send a PATCH request with a request body:
Send a DELETE request:
To add handlers for specific paths and HTTP methods you can create a new instance of the Handler
interface
and register it using the registerHandler
method of the Server
class.
For example:
Server server = new Server("localhost", 8081);
server.registerHandler("/hello", "GET", (req, res) -> {
res.sendText(200, "Hello, World!");
});