httpclient

package module
v0.2.0 Latest Latest
Warning

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

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

README

go-http-client 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
  • 可用於 PUT / PATCH / DELETE / GET
  • 簡單新增自訂 Headers
  • 內建 JSON PrettyPrint 輔助函數

安裝

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

使用範例

  1. POST JSON
payload := map[string]any{
    "name": "Tiger",
    "email": "tiger@example.com",
}

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": "tiger@example.com",
}

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)

輔助函數

  • PrettyPrint(body []byte)
    將 JSON 回傳格式化輸出,如果不是 JSON,則原樣輸出文字。

授權

MIT License

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewMultipart

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

func PrettyPrint(body []byte)

將 []byte 嘗試轉成 JSON Pretty Print

Types

type Request

type Request struct {
	Method  string
	URL     string
	Headers map[string]string
	Body    io.Reader
}

func NewRequest

func NewRequest(method, url string) *Request

建立 Request

func (*Request) Do

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

執行 request,回傳 body + status + error

func (*Request) Form

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

設定 form-urlencoded body

func (*Request) HeadersAdd

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

設定自訂 headers

func (*Request) JSON

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

設定 JSON body

func (*Request) Multipart

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