Skip to content

Commit 1c9e74d

Browse files
authored
fix: update doc
1 parent 29491f7 commit 1c9e74d

File tree

1 file changed

+91
-25
lines changed

1 file changed

+91
-25
lines changed

README.md

Lines changed: 91 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -8,25 +8,41 @@
88

99
axios4go is a Go HTTP client library inspired by Axios, providing a simple and intuitive API for making HTTP requests. It offers features like JSON handling, configurable instances, and support for various HTTP methods.
1010

11+
## Table of Contents
12+
13+
- [Features](#features)
14+
- [Installation](#installation)
15+
- [Usage](#usage)
16+
- [Making a Simple Request](#making-a-simple-request)
17+
- [Using Request Options](#using-request-options)
18+
- [Making POST Requests](#making-post-requests)
19+
- [Using Async Requests](#using-async-requests)
20+
- [Creating a Custom Client](#creating-a-custom-client)
21+
- [Configuration Options](#configuration-options)
22+
- [Contributing](#contributing)
23+
- [License](#license)
24+
1125
## Features
1226

1327
- Simple and intuitive API
14-
- Support for GET, POST, PUT, DELETE, HEAD, OPTIONS, and PATCH methods
28+
- Support for `GET`, `POST`, `PUT`, `DELETE`, `HEAD`, `OPTIONS`, and `PATCH` methods
1529
- JSON request and response handling
1630
- Configurable client instances
17-
- Timeout and redirect management
31+
- Global timeout and redirect management
1832
- Basic authentication support
1933
- Customizable request options
2034
- Promise-like asynchronous requests
2135

2236
## Installation
2337

24-
To install axios4go, use `go get`:
38+
To install `axios4go`, use `go get`:
2539

2640
```bash
27-
go get github.com/rezmoss/axios4go
41+
go get -u github.com/rezmoss/axios4go
2842
```
2943

44+
**Note**: Requires Go 1.13 or later.
45+
3046
## Usage
3147

3248
### Making a Simple Request
@@ -53,11 +69,13 @@ func main() {
5369
### Using Request Options
5470

5571
```go
56-
resp, err := axios4go.Get("https://api.example.com/data", &axios4go.requestOptions{
57-
timeout: 5000, // 5 seconds
58-
headers: map[string]string{
72+
resp, err := axios4go.Get("https://api.example.com/data", &axios4go.RequestOptions{
73+
Headers: map[string]string{
5974
"Authorization": "Bearer token",
6075
},
76+
Params: map[string]string{
77+
"query": "golang",
78+
},
6179
})
6280
```
6381

@@ -69,6 +87,12 @@ body := map[string]interface{}{
6987
"age": 30,
7088
}
7189
resp, err := axios4go.Post("https://api.example.com/users", body)
90+
if err != nil {
91+
fmt.Printf("Error: %v\n", err)
92+
return
93+
}
94+
fmt.Printf("Status Code: %d\n", resp.StatusCode)
95+
fmt.Printf("Body: %s\n", string(resp.Body))
7296
```
7397

7498
### Using Async Requests
@@ -90,33 +114,75 @@ axios4go.GetAsync("https://api.example.com/data").
90114
### Creating a Custom Client
91115

92116
```go
93-
client := axios4go.NewClient("https://api.example.com")
94-
resp, err := client.Request(&axios4go.requestOptions{
95-
method: "GET",
96-
url: "/users",
117+
client := axios4go.NewClient("https://api.example.com", 5*time.Second, 5) // Base URL, Timeout, Max Redirects
118+
119+
resp, err := client.Request(&axios4go.RequestOptions{
120+
Method: "GET",
121+
URL: "/users",
122+
Headers: map[string]string{
123+
"Authorization": "Bearer token",
124+
},
97125
})
126+
if err != nil {
127+
fmt.Printf("Error: %v\n", err)
128+
return
129+
}
130+
fmt.Printf("Status Code: %d\n", resp.StatusCode)
131+
fmt.Printf("Body: %s\n", string(resp.Body))
98132
```
99133

134+
**Note**: When creating a custom client, timeouts and max redirects are set at the client level.
135+
100136
## Configuration Options
101137

102-
axios4go supports various configuration options through the `requestOptions` struct:
138+
`axios4go` supports various configuration options through the `RequestOptions` struct:
103139

104-
- `method`: HTTP method (GET, POST, etc.)
105-
- `url`: Request URL
106-
- `baseURL`: Base URL for the request
107-
- `params`: URL parameters
108-
- `body`: Request body
109-
- `headers`: Custom headers
110-
- `timeout`: Request timeout in milliseconds
111-
- `auth`: Basic authentication credentials
112-
- `maxRedirects`: Maximum number of redirects to follow
113-
- `maxContentLength`: Maximum allowed response content length
114-
- `maxBodyLength`: Maximum allowed request body length
140+
- **Method**: HTTP method (`GET`, `POST`, etc.)
141+
- **URL**: Request URL (relative to `BaseURL` if provided)
142+
- **BaseURL**: Base URL for the request (overrides client's `BaseURL` if set)
143+
- **Params**: URL query parameters (`map[string]string`)
144+
- **Body**: Request body (can be `string`, `[]byte`, or any JSON serializable object)
145+
- **Headers**: Custom headers (`map[string]string`)
146+
- **Auth**: Basic authentication credentials (`&Auth{Username: "user", Password: "pass"}`)
147+
- **ValidateStatus**: Function to validate HTTP response status codes
148+
149+
**Example**:
150+
151+
```go
152+
options := &axios4go.RequestOptions{
153+
Method: "POST",
154+
URL: "/submit",
155+
Headers: map[string]string{
156+
"Content-Type": "application/json",
157+
},
158+
Body: map[string]interface{}{
159+
"title": "Sample",
160+
"content": "This is a sample post.",
161+
},
162+
Auth: &axios4go.Auth{
163+
Username: "user",
164+
Password: "pass",
165+
},
166+
Params: map[string]string{
167+
"verbose": "true",
168+
},
169+
ValidateStatus: func(statusCode int) bool {
170+
return statusCode >= 200 && statusCode < 300
171+
},
172+
}
173+
174+
resp, err := client.Request(options)
175+
```
115176

116177
## Contributing
117178

118-
Contributions to axios4go are welcome! Please feel free to submit a Pull Request.
179+
Contributions to `axios4go` are welcome! Please follow these guidelines:
180+
181+
- **Fork the repository** and create a new branch for your feature or bug fix.
182+
- **Ensure your code follows Go conventions** and passes all tests.
183+
- **Write tests** for new features or bug fixes.
184+
- **Submit a Pull Request** with a clear description of your changes.
119185

120186
## License
121187

122-
This project is licensed under the Apache License, Version 2.0 - see the [LICENSE](LICENSE) file for details.
188+
This project is licensed under the Apache License, Version 2.0. See the [LICENSE](LICENSE) file for details.

0 commit comments

Comments
 (0)