Generic Go Http Client

Gonet/httpGenericLoggingMultipartForm Data
Generic Go Http Client

Generic Go Http Client

A lightweight and generic HTTP client for Go, built to simplify HTTP requests and responses while maintaining flexibility. This package wraps Go's standard net/http client and introduces the power of generics, enabling you to define request and response types effortlessly.

✨ Features

  • Generic Request & Response: Use typed requests and responses for safer and more readable code.
  • Request Context: Fully supports context.Context to manage request deadlines and cancellations.
  • Request/Response Logging: Easily enable or disable logging for debugging or monitoring purposes.
  • Clean API: Simple, intuitive, and extendable.

🔧 Installation

go get github.com/harryosmar/http-client-go

🚀 Getting Started

Define Your Generic Request and Response

package main

import (
	"context"
	"fmt"
	"log"
	"github.com/harryosmar/http-client-go/v2"
)

type MyRequest struct {
	Name string `json:"name"`
}

type MyResponse struct {
	Greeting string `json:"greeting"`
}

func main() {
	client := http_client_go.NewHTTPClient()

	req := MyRequest{Name: "John Doe"}
	headers := map[string]string{
		"Content-Type": "application/json",
	}

	// Send POST request
	response, err := http_client_go.Post[MyRequest, MyResponse](
		context.Background(),
		client,
		"https://api.example.com/greet",
		req,
		headers,
	)
	if err != nil {
		log.Fatalf("Error: %v", err)
	}

	fmt.Printf("Response: %+v\n", response.Content)
	fmt.Printf("Duration: %dms\n", response.Duration)
}

📖 Available Methods

Generic Methods Overview

1. GET

Perform a GET request with optional query parameters.

Get[ResponseType any](ctx context.Context, client HttpClientRepository, url string, queries map[string][]string, headers map[string]string) (Response[ResponseType], error)

2. POST

Send a JSON-encoded POST request with a payload.

Post[RequestType any, ResponseType any](ctx context.Context, client HttpClientRepository, url string, payload RequestType, headers map[string]string) (Response[ResponseType], error)

3. POST Form-URL-Encoded

Send a POST request with form-url-encoded data.

PostFormUrlEncoded[ResponseType any](ctx context.Context, client HttpClientRepository, url string, payload url.Values, headers map[string]string) (Response[ResponseType], error)

4. POST Multipart

Upload files using multipart/form-data.

PostMultipart[ResponseType any](ctx context.Context, client HttpClientRepository, url string, file *os.File, headers map[string]string) (Response[ResponseType], error)

5. PUT

Send a JSON-encoded PUT request with a payload.

Put[RequestType any, ResponseType any](ctx context.Context, client HttpClientRepository, url string, payload RequestType, headers map[string]string) (Response[ResponseType], error)

6. DELETE

Perform a DELETE request.

Delete[ResponseType any](ctx context.Context, client HttpClientRepository, url string, headers map[string]string) (Response[ResponseType], error)

📦 Error Handling

Errors are wrapped in a ResponseErr struct to provide detailed information about failed requests:

  • Content: Error details, parsed if possible.
  • StatusCode: HTTP status code of the response.
  • Duration: Time taken for the request (ms).
  • Header: HTTP response headers.

🛠️ Advanced Features

Raw Payload Support

Send raw []byte data using PostRaw.

rawPayload := []byte(`{"key": "value"}`)
response, err := http_client_go.PostRaw[MyResponse](ctx, client, url, rawPayload, headers)