httpclient

package module
v1.2.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 29, 2025 License: MIT Imports: 10 Imported by: 1

README

http-client

Universal HTTP client for Go with method chaining, multipart support, and zero third-party dependencies.

Features

  • Method chaining for building requests
  • Multipart/form-data file uploads
  • Header and query param management
  • Thread-safe, buffer pooling for performance
  • Minimal API, no external dependencies

Usage Example

package main

import (
    "context"
    "log"
    "net/http"
    "github.com/nativebpm/http-client"
)

func main() {
    client, err := httpclient.NewClient(&http.Client{}, "https://api.example.com")
    if err != nil {
        log.Fatal(err)
    }

    resp, err := client.RequestPOST(context.Background(), "/endpoint").
        Header("Authorization", "Bearer token").
        JSONBody(map[string]string{"key": "value"}).
        Send()
    if err != nil {
        log.Fatal(err)
    }
    defer resp.Body.Close()
    // ... handle resp ...
}
Multipart Example
resp, err := client.MultipartPOST(ctx, "/upload").
    FormField("desc", "test").
    File("file", "file.txt", strings.NewReader("hello")).
    Send()

API

Client:

  • NewClient(httpClient *http.Client, baseURL string) (*Client, error)
  • RequestGET/POST/PUT/PATCH/DELETE(ctx, path) *Request
  • MultipartPOST(ctx, path) *Multipart

Request:

  • Header(key, value string) *Request
  • QueryParam(key, value string) *Request
  • JSONBody(body any) *Request
  • Send() (*http.Response, error)

Multipart:

  • FormField(fieldName, value string) *Multipart
  • File(fieldName, filename string, content io.Reader) *Multipart
  • Header(key, value string) *Multipart
  • Send() (*http.Response, error)

Testing

Run all tests and benchmarks:

go test -v -bench=. ./...

Project Structure

  • httpclient.go — main client and request logic
  • request.go — request builder, JSON, query, headers
  • multipart.go — multipart/form-data support
  • httpclient_test.go — tests and benchmarks

License

MIT — see LICENSE

Documentation

Index

Constants

View Source
const (
	ApplicationJSON = "application/json"
	ContentType     = "Content-Type"
	ContentLength   = "Content-Length"
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Client

type Client struct {
	// contains filtered or unexported fields
}

func NewClient

func NewClient(client *http.Client, baseURL string) (*Client, error)

func (*Client) MultipartPOST added in v1.2.0

func (c *Client) MultipartPOST(ctx context.Context, path string) *Multipart

func (*Client) NewMultipartRequest added in v1.2.0

func (c *Client) NewMultipartRequest(ctx context.Context, method, path string) *Multipart

func (*Client) NewRequest added in v1.2.0

func (c *Client) NewRequest(ctx context.Context, method, path string) *Request

func (*Client) RequestDELETE added in v1.2.0

func (c *Client) RequestDELETE(ctx context.Context, path string) *Request

func (*Client) RequestGET added in v1.2.0

func (c *Client) RequestGET(ctx context.Context, path string) *Request

func (*Client) RequestPATCH added in v1.2.0

func (c *Client) RequestPATCH(ctx context.Context, path string) *Request

func (*Client) RequestPOST added in v1.2.0

func (c *Client) RequestPOST(ctx context.Context, path string) *Request

func (*Client) RequestPUT added in v1.2.0

func (c *Client) RequestPUT(ctx context.Context, path string) *Request

type Multipart added in v1.1.0

type Multipart struct {
	*Client
	// contains filtered or unexported fields
}

func (*Multipart) ContentType added in v1.1.0

func (r *Multipart) ContentType(contentType string) *Multipart

func (*Multipart) File added in v1.1.0

func (r *Multipart) File(fieldName, filename string, content io.Reader) *Multipart

func (*Multipart) FormField added in v1.1.0

func (r *Multipart) FormField(fieldName, value string) *Multipart

func (*Multipart) GetRequest added in v1.1.0

func (r *Multipart) GetRequest() (*Multipart, error)

func (*Multipart) Header added in v1.1.0

func (r *Multipart) Header(key, value string) *Multipart

func (*Multipart) Headers added in v1.1.0

func (r *Multipart) Headers(headers map[string]string) *Multipart

func (*Multipart) Send added in v1.1.0

func (r *Multipart) Send() (*http.Response, error)

type Request

type Request struct {
	*Client
	// contains filtered or unexported fields
}

func (*Request) Body

func (r *Request) Body(body io.ReadCloser) *Request

func (*Request) BytesBody

func (r *Request) BytesBody(body []byte) *Request

func (*Request) ContentType

func (r *Request) ContentType(contentType string) *Request

func (*Request) GetRequest added in v0.0.4

func (r *Request) GetRequest() (*Request, error)

func (*Request) Header

func (r *Request) Header(key, value string) *Request

func (*Request) Headers

func (r *Request) Headers(headers map[string]string) *Request

func (*Request) JSONBody

func (r *Request) JSONBody(body any) *Request

func (*Request) QueryParam

func (r *Request) QueryParam(key, value string) *Request

func (*Request) QueryParams

func (r *Request) QueryParams(params map[string]string) *Request

func (*Request) QueryValues

func (r *Request) QueryValues(values url.Values) *Request

func (*Request) Send

func (r *Request) Send() (*http.Response, error)

func (*Request) StringBody

func (r *Request) StringBody(body string) *Request

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL