httpclient

package module
v1.4.9 Latest Latest
Warning

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

Go to latest
Published: Oct 2, 2025 License: MIT Imports: 5 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
  • 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").
        JSON(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").
    Param("desc", "test").
    File("file", "file.txt", strings.NewReader("hello")).
    Send()

API

Client:

  • NewClient(httpClient *http.Client, baseURL string) (*Client, error)
  • RequestGET(ctx, path) *Request
  • RequestPOST(ctx, path) *Request
  • RequestPUT(ctx, path) *Request
  • RequestPATCH(ctx, path) *Request
  • RequestDELETE(ctx, path) *Request
  • MultipartPOST(ctx, path) *Multipart
  • MultipartPUT(ctx, path) *Multipart

Request:

  • Header(key, value string) *Request
  • Param(key, value string) *Request (adds query param)
  • Bool(fieldName string, value bool) *Request (adds query param)
  • Float(fieldName string, value float64) *Request (adds query param)
  • Body(body io.ReadCloser) *Request
  • Bytes(body []byte) *Request
  • JSON(body any) *Request
  • Send() (*http.Response, error)

Multipart:

  • Header(key, value string) *Multipart
  • Param(key, value string) *Multipart (adds form field)
  • Bool(fieldName string, value bool) *Multipart (adds form field)
  • Float(fieldName string, value float64) *Multipart (adds form field)
  • File(fieldName, filename string, content io.Reader) *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/request.go — request builder, JSON, query, headers
  • request/multipart.go — multipart/form-data support
  • httpclient_test.go, request/request_test.go, request/multipart_test.go — tests and benchmarks

License

MIT — see LICENSE

Documentation

Overview

Package httpclient provides a convenient HTTP client with request builders. It supports both regular and multipart requests with deferred operations.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Client

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

Client is an HTTP client that wraps http.Client with convenience methods. It maintains a base URL and provides request builders for different HTTP methods.

func NewClient

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

NewClient creates a new HTTP client with the given http.Client and base URL. Returns an error if the base URL is invalid.

func (*Client) MultipartPOST added in v1.2.0

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

MultipartPOST creates a multipart POST request builder for the given path.

func (*Client) MultipartPUT added in v1.3.0

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

MultipartPUT creates a multipart PUT request builder for the given path.

func (*Client) RequestDELETE added in v1.2.0

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

RequestDELETE creates a DELETE request builder for the given path.

func (*Client) RequestGET added in v1.2.0

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

RequestGET creates a GET request builder for the given path.

func (*Client) RequestPATCH added in v1.2.0

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

RequestPATCH creates a PATCH request builder for the given path.

func (*Client) RequestPOST added in v1.2.0

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

RequestPOST creates a POST request builder for the given path.

func (*Client) RequestPUT added in v1.2.0

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

RequestPUT creates a PUT request builder for the given path.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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