Powerful package for quick and simple HTTP requests in Go language.
- Why use r
- How to install
- Features
- API
- Getting Started
- Quick requests
- Advance requests
- Contribute and things ToDo
r takes all the power that language Go implements about http requests and makes all the work simpler and cleaner. There is no need to use special and complex types as http.Header and url.Value. Things like reading the response body, cookies, Headers and conversion from Json to custom type are now a simple tasks.
- Extremely simple to use.
- Make your code cleaner.
- Used simple types like string, map and bytes.
- Wrapping the common error handling for you.
For install use the go get tool, just type this in your terminal:
go get github.com/AmarShaked/r
###API
Let’s try to get a webpage. For this example, let’s get GitHub’s public timeline
res, err := r.Get('https://api.github.com/events')
// Some error handling
Now, we have a Response (not a regular http.Response) type called res. We can get all the information we need from this type. For example, to print the response body we will have to just do this:
res.Text() // Get the response body as string
If we try to do this without r package, we will have to write something like this:
resp, err := http.Get("https://api.github.com/events")
// Some error handling
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
// Error handling again
string(body)
We can send a quick requests of all HTTP requests types. Usually we use quick requests in case we need to send a request with no special settings.
GET, HEAD, OPTIONS are the easiest:
r, _ := r.Get('http://httpbin.org/get')
r, _ := r.Options('http://httpbin.org/get')
r, _ := r.Head('http://httpbin.org/get')
In POST method, we get map of strings, and we use it like a PostForm.
data := map[string]string{"test": "shaked", "test2": "shaked2"}
r, _ := r.Post('http://httpbin.org/post', data)
// "form": {
// "test": "shaked",
// "test2": "shaked2"
// }
PUT and DELETE receive a string for the body:
r, _ := r.Put('http://httpbin.org/put', "Test Body")
r, _ := r.Delete('http://httpbin.org/delete', "Test Body")
Advanced requests are designed for situations in which we want to add more things on the request. For example: basic authentication, Headers, cookies, etc.
req := &r.Request{Method: "GET",
Url: "http://httpbin.org/get"}
res, err := req.Do()
We do not have to provide a method to the request, but to use special functions that the type offer. The Request type provides us functions for all types of methods.
req := &r.Request{Url: "http://httpbin.org/get"}
res, err := req.Get()
res, err := req.Post()
res, err := req.Put()
res, err := req.Delete()
res, err := req.Options()
res, err := req.Head()
To add a Authentication header, just pass map of strings with your credentials.
req := &r.Request{Url: "http://httpbin.org/get",
Auth: []string{"username","password"}
res, err := req.Post()
In the body value we can put, string, bytes, io.Reader, and custom types that will parse as Json.
req := &r.Request{Url: "http://httpbin.org/get", Body: "string Body"}
req := &r.Request{Url: "http://httpbin.org/get", Body: []byte("bytes Body")}
req := &r.Request{Url: "http://httpbin.org/get", Body: strings.NewReader("string Body")}
type Animal struct {
Name
}
req := &r.Request{Url: "http://httpbin.org/get", Body: &Animal{Name: "dog"}}
res, err := req.Post()
The headers value gets a map of strings with all the header the you want to put in your request.
hdrs := map[string]string{"Header1": "Header 1 Value", "Header2": "Header 2 value"}
req := &r.Request{Url: "http://httpbin.org/get",
Headers: hdrs}
res, err := req.Post()
We have many more things to add to the package. We work on it all the time and we need your help.
- Check for open issues or open a fresh issue to start a discussion around a feature idea or a bug.
- Fork the repository on GitHub to start making your changes to the master branch (or branch off of it).
- Send email to [email protected] for any questions you may have.
- Give as a star ;)