This code provides a simple HTTP wrapper for making GET and POST requests. It includes classes and methods to perform HTTP requests and handle the response.
The SimpleHttpResponse
class represents the response from an HTTP request. It contains the following methods:
getStatusCode()
: Returns the status code of the response.getData()
: Returns the data (response body) of the response.
The SimpleHttpWrapper
class provides static methods to make HTTP requests. It includes the following methods:
get(String urlStr, String[] headers)
: Performs a GET request to the specified URL with optional headers and returns aSimpleHttpResponse
.post(String urlStr, String[] headers, String postData)
: Performs a POST request to the specified URL with optional headers and post data, and returns aSimpleHttpResponse
.performRequest(SupportedHttpMethod method, String urlStr, String[] headers, String postData)
: Performs an HTTP request with the specified method (GET or POST), URL, headers, and post data. Returns aSimpleHttpResponse
.performRequestUnsupported(String method, String urlStr, String[] headers, String postData)
: Discouraged method for performing an HTTP request with an unsupported method. Returns aSimpleHttpResponse
.
The SupportedHttpMethod
enum defines the supported HTTP methods. It includes the following methods:
getRawMethodName()
: Returns the raw method name as aString
.
To use the SimpleHttpWrapper
class, import the com.github.lightlibs.simplehttpwrapper
package and call the desired static methods. For example:
import com.github.lightlibs.simplehttpwrapper.SimpleHttpWrapper;
import com.github.lightlibs.simplehttpwrapper.SimpleHttpResponse;
class Example {
public static void main(String[] args) {
// Perform a GET request
SimpleHttpResponse response = SimpleHttpWrapper.get("https://example.com", null);
int statusCode = response.getStatusCode();
String data = response.getData();
// Perform a POST request with headers and post data
String[] headers = {"Content-Type: application/json"};
String postData = "{ \"name\": \"John\", \"age\": 30 }";
SimpleHttpResponse response = SimpleHttpWrapper.post("https://example.com", headers, postData);
int statusCode = response.getStatusCode();
String data = response.getData();
}
}
repositories {
maven { url 'https://jitpack.io' }
}
dependencies {
implementation 'com.github.lightlibs:simplehttpwrapper:[VERSION-STRING]'
}
<repositories>
<repository>
<id>jitpack.io</id>
<url>https://jitpack.io</url>
</repository>
</repositories>
<dependency>
<groupId>com.github.lightlibs</groupId>
<artifactId>simplehttpwrapper</artifactId>
<version>[VERSION-STRING]</version>
</dependency>
Note: Make sure to handle IOException
when making the requests.
Feel free to customize the code or add additional functionality as needed. And make a PR ;)