-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
doc: update readme with more examples
- Loading branch information
Showing
1 changed file
with
106 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,56 +6,130 @@ | |
This is module to help you make web requests in Go, it is a wrapper around the standard library's `http` package. | ||
You can use Get or Post to make a request, and then use the `Response` object to get the response body, headers, status code, etc. | ||
|
||
### Example | ||
|
||
package main | ||
|
||
import ( | ||
"fmt" | ||
"github.com/tonnytg/webreq" | ||
) | ||
|
||
func main() { | ||
response, err := webreq.Get("https://api.example.com/data") | ||
if err != nil { | ||
fmt.Println("Error:", err) | ||
return | ||
} | ||
fmt.Println("Response:", response) | ||
} | ||
|
||
|
||
## Why | ||
|
||
So many times I needed to make a request in an API and after convert body to struct, and every time I needed to configure http.Client and put headers and body. Thinking about "don't repeat your self" I believe this unified code can gain time build an easy way to make this request. | ||
|
||
What do you think? Liked, set star in this project to help me to help others! | ||
|
||
|
||
### Install | ||
## Install | ||
|
||
> go get github.com/tonnytg/webreq | ||
## Basic Usage | ||
|
||
### Get | ||
|
||
Create a slice of headers to use and input in request | ||
|
||
headers := webreq.NewHeaders() | ||
headers.Add("Content-Type", "application/json") | ||
|
||
request := webreq.Builder("GET") | ||
request.SetURL("https://610aa52552d56400176afebe.mockapi.io/api/v1/friendlist") | ||
|
||
_, err := request.Execute() | ||
if err != nil { | ||
t.Error(err) | ||
} | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"github.com/tonnytg/webreq" | ||
) | ||
|
||
func main() { | ||
response, err := webreq.Get("https://api.example.com/data") | ||
if err != nil { | ||
fmt.Println("Error:", err) | ||
return | ||
} | ||
fmt.Println("Response:", response) | ||
} | ||
|
||
|
||
### Post | ||
|
||
Create a body data to send in request | ||
|
||
... | ||
f := Friend{ | ||
CreatedAt: time.Now(), | ||
Name: "Tonny", | ||
} | ||
|
||
// convert f to bytes | ||
fBytes, err := json.Marshal(f) | ||
if err != nil { | ||
t.Error(err) | ||
} | ||
|
||
request := webreq.Builder("POST") | ||
request.SetURL("https://610aa52552d56400176afebe.mockapi.io/api/v1/friendlist") | ||
request.SetBody(fBytes) | ||
|
||
body, err := request.Execute() | ||
if err != nil { | ||
t.Error(err) | ||
} | ||
... | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"github.com/tonnytg/webreq" | ||
) | ||
|
||
func main() { | ||
data := map[string]string{"key": "value"} | ||
response, err := webreq.Post("https://api.example.com/data", data) | ||
if err != nil { | ||
fmt.Println("Error:", err) | ||
return | ||
} | ||
fmt.Println("Response:", response) | ||
} | ||
|
||
|
||
## Advange Usage | ||
|
||
|
||
### Custom Headers | ||
|
||
headers := map[string]string{ | ||
"Content-Type": "application/json", | ||
"Authorization": "Bearer your_token", | ||
} | ||
|
||
response, err := webreq.GetWithHeaders("https://api.example.com/data", headers) | ||
if err != nil { | ||
fmt.Println("Error:", err) | ||
return | ||
} | ||
fmt.Println("Response:", response) | ||
|
||
|
||
|
||
### Erro Handlin | ||
|
||
response, err := webreq.Get("https://api.example.com/data") | ||
if err != nil { | ||
switch err.(type) { | ||
case webreq.HTTPError: | ||
fmt.Println("HTTP error occurred:", err) | ||
default: | ||
fmt.Println("An error occurred:", err) | ||
} | ||
return | ||
} | ||
fmt.Println("Response:", response) | ||
|
||
|
||
## Contributing | ||
- Fork the repository | ||
- Create a new branch (git checkout -b feature-foo) | ||
- Commit your changes (git commit -am 'Add some foo') | ||
- Push to the branch (git push origin feature-foo) | ||
- Create a new Pull Request | ||
|
||
|
||
## License | ||
This project is licensed under the MIT License. | ||
|
||
|
||
## Contact | ||
|
||
For any inquiries, please open an issue or reach out to the maintainer. | ||
|
||
email: [email protected] |