The httpclient-call-go
library simplifies making HTTP calls to various API services efficiently and straightforwardly.
It is designed to seamlessly integrate into any Go project requiring HTTP API interactions.
Thank you for your support! ❤️
- Easy HTTP client configuration.
- Full support for customizing HTTP requests (headers, body, timeouts).
- Convenient methods for making HTTP calls and deserializing JSON responses.
To use httpclient-call-go
in your project, install it using the following Go command:
go get github.com/pzentenoe/httpclient-call-go
First, import the library and create a new instance of HTTPClientCall specifying the base URL of the API service and an HTTP client:
import (
"net/http"
client "github.com/pzentenoe/httpclient-call-go"
)
httpClientCall := client.NewHTTPClientCall("https://dummyhost.cl", &http.Client{})
Using Do Implementation
To perform a simple POST request and handle the response as []byte:
package main
import (
"context"
"fmt"
"io"
"net/http"
"time"
client "github.com/pzentenoe/httpclient-call-go"
)
func main() {
httpClientCall := client.NewHTTPClientCall("https://dummyhost.cl", &http.Client{})
headers := http.Header{
client.HeaderContentType: []string{client.MIMEApplicationJSON},
}
dummyBody := map[string]interface{}{"age": 30, "name": "test"}
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
response, err := httpClientCall.
Method(http.MethodPost).
Path("/path").
Body(dummyBody).
Headers(headers).
Do(ctx)
if err != nil {
fmt.Println("Error calling the API:", err)
return
}
defer response.Body.Close()
dataBytes, errToRead := io.ReadAll(response.Body)
if errToRead != nil {
fmt.Println("Error reading data:", errToRead)
return
}
fmt.Println(string(dataBytes))
}
Using DoWithUnmarshal Implementation
To perform a POST request and automatically deserialize the JSON response into a Go structure:
package main
import (
"context"
"fmt"
"net/http"
"time"
client "github.com/pzentenoe/httpclient-call-go"
)
type someBodyResponse struct {
Name string `json:"name"`
}
func main() {
httpClientCall := client.NewHTTPClientCall("https://dummyhost.cl", &http.Client{})
headers := http.Header{
client.HeaderContentType: []string{client.MIMEApplicationJSON},
}
dummyBody := map[string]interface{}{"age": 30, "name": "test"}
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
var responseBody someBodyResponse
resp, err := httpClientCall.
Method(http.MethodPost).
Path("/path").
Body(dummyBody).
Headers(headers).
DoWithUnmarshal(ctx, &responseBody)
if err != nil {
fmt.Println("Error calling the API:", err)
return
}
fmt.Println("Status Code:", resp.StatusCode)
fmt.Println("Name in Response:", responseBody.Name)
}
Execute the tests with:
go test ./...
We welcome contributions! Please fork the project and submit pull requests to the main
branch. Make sure to add tests
for new functionalities and document any significant changes.
This project is released under the MIT License. See the LICENSE file for more details.
For a detailed changelog, refer to CHANGELOG.md.
- Pablo Zenteno - Full Stack Developer - pzentenoe