https://github.com/sigpwned/httpmodel
A library-agnostic HTTP library for library-agnostic HTTP operations for Java 8+
https://github.com/sigpwned/httpmodel
http http-client java
Last synced: about 2 months ago
JSON representation
A library-agnostic HTTP library for library-agnostic HTTP operations for Java 8+
- Host: GitHub
- URL: https://github.com/sigpwned/httpmodel
- Owner: sigpwned
- Created: 2022-05-16T16:09:00.000Z (almost 4 years ago)
- Default Branch: main
- Last Pushed: 2024-04-01T06:10:24.000Z (almost 2 years ago)
- Last Synced: 2025-01-20T14:53:17.844Z (about 1 year ago)
- Topics: http, http-client, java
- Language: Java
- Homepage:
- Size: 286 KB
- Stars: 1
- Watchers: 3
- Forks: 0
- Open Issues: 7
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# HTTPMODEL [](https://github.com/sigpwned/httpmodel/actions/workflows/tests.yml) [](https://search.maven.org/artifact/com.sigpwned/httpmodel)
## Motivation
Many HTTP operations, like signing requests, are library agnostic. Implementing these operations against multiple libraries is wasteful. The httpmodel library is designed to allow library writers to build library-agnostic models once, and then simply allow users to convert their models back and forth between the httpmodel object model.
The httpmodel library also offers a client SPI with interchangeable transport backends for those who prefer to do work using the model itself.
## Goals
* Define a library-agnostic model of HTTP primitive objects, like requests, headers, etc.
* Provide conversion routines between the model and the most popular HTTP client and server libraries
* Expose a simple HTTP client to use the model directly
## Non-Goals
* Build a new HTTP implementation
* Support all HTTP libraries
## Conversion Examples
### Servlets
Users can convert back and forth between servlet objects like this:
public ModelHttpRequest convertToHttpModel(HttpServletRequest request) {
return ModelHttpServlets.fromRequest(request);
}
public HttpServletResponse populateServletResponse(HttpModelResponse response, HttpServletResponse s) {
return ModelHttpServlets.toResponse(s, response);
}
This requires the `httpmodel-servlet` module.
### Java 11 Client
Users can convert back and forth between Java 11 `HttpClient` objects like this:
public HttpRequest convertToHttpRequest(ModelHttpRequest request) {
return ModelHttpClients.toRequest(request);
}
public ModelHttpResponse convertFromHttpResponse(HttpResponse response) {
return ModelHttpClients.fromResponse(response);
}
This requires the `httpmodel-core` module.
### HttpURLConnection
Users can convert back and forth between `HttpURLConnection` objects like this:
public HttpURLConnection convertToHttpURLConnection(ModelHttpRequest request) {
return ModelHttpURLConnections.toRequest(request);
}
public ModelHttpResponse convertFromHttpURLConnection(HttpURLConnection connection) {
return ModelHttpURLConnections.fromResponse(connection);
}
This requires the `httpmodel-core` module.
## HTTP Client Examples
To grab a copy of the Yahoo! frontpage, use this code:
try (ModelHttpClient client=new DefaultModelHttpClient(new UrlConnectionModelHttpConnector())) {
try (ModelHttpResponse response=client.send(ModelHttpRequest.builder()
.method(ModelHttpMethods.GET)
.url(URI.create("https://www.yahoo.com/"))
.build())) {
System.out.println(response.getStatusCode());
System.out.println(response.toString(StandardCharsets.UTF_8));
}
}
This requires the `httpmodel-client` module.