httpclient

package module
v1.2.1 Latest Latest
Warning

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

Go to latest
Published: Jan 24, 2026 License: MIT Imports: 11 Imported by: 0

README

go-httpclient Go version

/
├── go.mod
├── client.go        # 核心 Client + DoRequest
├── request.go       # 主要 Request struct + 鏈式呼叫方法
├── multipart.go     # 共用 multipart 工具
├── decode.go        # JSON Decode / PrettyPrint helper

一個輕量級的 Go HTTP 客戶端,支援 JSON、Form 以及 Multipart 請求。
適合 restful web service client,提供鏈式呼叫 API,讓 HTTP 請求寫法乾淨、統一。


功能特色

  • 鏈式呼叫 Request
  • 支援 JSON / Form / Multipart
  • 可用於 GET / POST / PUT / PATCH / DELETE
  • 簡單新增自訂 Headers
  • 內建 JSON PrettyPrint 輔助函數
  • 內建 GetJSONField 輔助函數
  • 預設 timeout 時間為:10 秒,可以自行更改設定

安裝

go get github.com/tiger586/go-httpclient

使用範例

  1. POST JSON
payload := map[string]any{
    "name": "Tiger",
    "email": "[email protected]",
}

headers := map[string]string{
    "Authorization": "Bearer abc123",
    "Accept": "application/json",
}

body, status, err := httpclient.NewRequest("POST", "https://httpbin.org/post").
    JSON(payload).
    HeadersAdd(headers).
    Do()
if err != nil {
    log.Fatal(err)
}

httpclient.PrettyPrint(body)
  1. POST Form
form := map[string]string{
    "name": "Tiger",
    "email": "[email protected]",
}

body, status, _ = httpclient.NewRequest("POST", "https://httpbin.org/post").
    Form(form).
    Do()

httpclient.PrettyPrint(body)
  1. POST Multipart (fields + files 含欄位與檔案)
fields := map[string]string{
    "name": "Tiger",
}

files := map[string]string{
    "file": "./test.png",
}

body, status, _ = httpclient.NewRequest("POST", "https://httpbin.org/post").
    Multipart(fields, files).
    Do()

httpclient.PrettyPrint(body)

// 自訂 timeout
client := httpclient.NewClient(100 * time.Millisecond)
body, status, err = client.NewRequest("POST", "https://httpbin.org/post").
    JSON(payload).
    HeadersAdd(headers).
    Do()
if err != nil {
    log.Fatal(err)
}

httpclient.PrettyPrint(body)

輔助函數

  • PrettyPrint(body []byte)
    將 JSON 回傳格式化輸出,如果不是 JSON,則原樣輸出文字。
  • GetJSONField(body []byte, key string) string
    直接從返回的 body 取值,支援巢狀 key,例如 "data.user.name"
    如果 body 不是 JSON 或 key 不存在,回傳空字串。

授權

MIT License

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GetJSONField added in v1.1.1

func GetJSONField(body []byte, key string) string

GetJSONField 支援巢狀 key,例如 "data.user.name" 如果 body 不是 JSON 或 key 不存在,回傳空字串

func NewMultipart added in v1.0.0

func NewMultipart(fields map[string]string, files map[string]string) (*bytes.Buffer, string, error)

NewMultipart 建立 multipart body fields: form fields, files: key -> file path

func PrettyPrint added in v1.0.0

func PrettyPrint(body []byte)

將 []byte 嘗試轉成 JSON Pretty Print

Types

type Client added in v1.2.1

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

func NewClient added in v1.2.1

func NewClient(timeout time.Duration) *Client

建構函數,自訂 timeout

func (*Client) NewRequest added in v1.2.1

func (c *Client) NewRequest(method, url string) *Request

type Request added in v1.0.0

type Request struct {
	Method  string
	URL     string
	Headers map[string]string
	Body    io.Reader
	// contains filtered or unexported fields
}

func NewRequest added in v1.0.0

func NewRequest(method, url string) *Request

建立 Request

method = GET / POST / PUT / PATCH / DELETE NewRequest 保留舊用法,使用預設 client(10 秒 timeout)

func NewRequest(method, url string) *Request {
	method = strings.ToUpper(method) // ✅ 自動轉大寫
	return &Request{
		Method:  method,
		URL:     url,
		Headers: map[string]string{},
	}
}

func (*Request) Do added in v1.0.0

func (r *Request) Do() ([]byte, int, error)

執行 request,回傳 body + status + error

func (*Request) Form added in v1.0.0

func (r *Request) Form(form map[string]string) *Request

設定 form-urlencoded body

func (*Request) HeadersAdd added in v1.0.0

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

設定自訂 headers

func (*Request) JSON added in v1.0.0

func (r *Request) JSON(payload any) *Request

設定 JSON body

func (*Request) Multipart added in v1.0.0

func (r *Request) Multipart(fields map[string]string, files map[string]string) *Request

設定 multipart body

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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