|
| 1 | +# Simple HTTP Wrapper |
| 2 | + |
| 3 | +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. |
| 4 | + |
| 5 | +## SimpleHttpResponse |
| 6 | + |
| 7 | +The `SimpleHttpResponse` class represents the response from an HTTP request. It contains the following methods: |
| 8 | + |
| 9 | +- `getStatusCode()`: Returns the status code of the response. |
| 10 | +- `getData()`: Returns the data (response body) of the response. |
| 11 | + |
| 12 | +## SimpleHttpWrapper |
| 13 | + |
| 14 | +The `SimpleHttpWrapper` class provides static methods to make HTTP requests. It includes the following methods: |
| 15 | + |
| 16 | +- `get(String urlStr, String[] headers)`: Performs a GET request to the specified URL with optional headers and returns a `SimpleHttpResponse`. |
| 17 | +- `post(String urlStr, String[] headers, String postData)`: Performs a POST request to the specified URL with optional headers and post data, and returns a `SimpleHttpResponse`. |
| 18 | +- `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 a `SimpleHttpResponse`. |
| 19 | +- `performRequestUnsupported(String method, String urlStr, String[] headers, String postData)`: Discouraged method for performing an HTTP request with an unsupported method. Returns a `SimpleHttpResponse`. |
| 20 | + |
| 21 | +## SupportedHttpMethod |
| 22 | + |
| 23 | +The `SupportedHttpMethod` enum defines the supported HTTP methods. It includes the following methods: |
| 24 | + |
| 25 | +- `getRawMethodName()`: Returns the raw method name as a `String`. |
| 26 | + |
| 27 | +## Usage |
| 28 | + |
| 29 | +To use the `SimpleHttpWrapper` class, import the `com.github.lightlibs.simplehttpwrapper` package and call the desired static methods. For example: |
| 30 | + |
| 31 | +```java |
| 32 | +import com.github.lightlibs.simplehttpwrapper.SimpleHttpWrapper; |
| 33 | +import com.github.lightlibs.simplehttpwrapper.SimpleHttpResponse; |
| 34 | + |
| 35 | +class Example { |
| 36 | + |
| 37 | + public static void main(String[] args) { |
| 38 | + // Perform a GET request |
| 39 | + SimpleHttpResponse response = SimpleHttpWrapper.get("https://example.com", null); |
| 40 | + int statusCode = response.getStatusCode(); |
| 41 | + String data = response.getData(); |
| 42 | + |
| 43 | + // Perform a POST request with headers and post data |
| 44 | + String[] headers = {"Content-Type: application/json"}; |
| 45 | + String postData = "{ \"name\": \"John\", \"age\": 30 }"; |
| 46 | + SimpleHttpResponse response = SimpleHttpWrapper.post("https://example.com", headers, postData); |
| 47 | + int statusCode = response.getStatusCode(); |
| 48 | + String data = response.getData(); |
| 49 | + } |
| 50 | + |
| 51 | +} |
| 52 | +``` |
| 53 | + |
| 54 | +Note: Make sure to handle `IOException` when making the requests. |
| 55 | + |
| 56 | +Feel free to customize the code or add additional functionality as needed. And make a PR ;) |
0 commit comments