ninedraft / httpclient

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

httpclient

codecov

httpclient is a thin wrapper around http.Client with some useful features.

  • methods: .Post, .Get, .Put, .Delete, etc.
  • simplified JSON, form and multipart requests

Installation

go get -v github.com/ninedraft/httpclient@latest

Usage

Simple POST request

resp, err := client.Post(ctx, "https://httpbin.org/post", "application/json",
		strings.NewReader(`{"foo": "bar"}`))

JSON request

resp, err := client.PostJSON(ctx, "https://httpbin.org/post", map[string]string{"foo": "bar"})

Form request

resp, err := client.PostForm(ctx, "https://httpbin.org/post", 
    url.Values{
        "foo": {"bar"}",
    })

Multipart request

resp, err = client.PostMultipart(ctx, "https://httpbin.org/post",
    httpclient.WriteMultiparts(
	    httpclient.MultipartFields(url.Values{
		    "foo": []string{"bar"},
		}),
        httpclient.MultipartFile("file", "file.txt", strings.NewReader("file content")),
    ))

About

License:Apache License 2.0


Languages

Language:Go 100.0%