httputil

package module
v0.0.0-...-e417201 Latest Latest
Warning

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

Go to latest
Published: Jul 14, 2025 License: MIT Imports: 18 Imported by: 0

README

codecov Build Status go.dev Go Report Card Licenses

httputil

golang http utils for common use

Basic Usage

Installation

To get the package, execute:

go get github.com/gofika/httputil
Example
package main

import (
	"fmt"

	"github.com/gofika/httputil"
)

func main() {
	resp, err := httputil.Get("https://httpbin.org/get")
	if err != nil {
		panic(err)
	}
	type GetResp struct {
		URL string `json:"url"`
	}
	res, err := httputil.ReadJSON[GetResp](resp)
	fmt.Printf("url=%s\n", res.URL)
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Delete

func Delete(url string, opts ...RequestOption) (resp *http.Response, err error)

Delete issues a DELETE to the specified URL.

When err is nil, resp always contains a non-nil resp.Body. Caller should close resp.Body when done reading from it.

func Get

func Get(url string, opts ...RequestOption) (resp *http.Response, err error)

Get issues a GET to the specified URL.

When err is nil, resp always contains a non-nil resp.Body. Caller should close resp.Body when done reading from it.

func Head(url string, opts ...RequestOption) (resp *http.Response, err error)

Head issues a HEAD to the specified URL.

When err is nil, resp always contains a non-nil resp.Body. Caller should close resp.Body when done reading from it.

func Patch

func Patch(url string, contentType string, body io.Reader, opts ...RequestOption) (resp *http.Response, err error)

Patch issues a PATCH to the specified URL.

When err is nil, resp always contains a non-nil resp.Body. Caller should close resp.Body when done reading from it.

func PatchJSON

func PatchJSON(url string, body any, opts ...RequestOption) (resp *http.Response, err error)

PatchJSON issues a PATCH to the specified URL with the given body as JSON. values JSON as the request body.

The Content-Type header is set to application/json.

When err is nil, resp always contains a non-nil resp.Body. Caller should close resp.Body when done reading from it.

func Post

func Post(url string, contentType string, body io.Reader, opts ...RequestOption) (resp *http.Response, err error)

Post issues a POST to the specified URL.

When err is nil, resp always contains a non-nil resp.Body. Caller should close resp.Body when done reading from it.

func PostForm

func PostForm(url string, data url.Values, opts ...RequestOption) (resp *http.Response, err error)

PostForm issues a POST to the specified URL, with data's keys and values URL-encoded as the request body.

The Content-Type header is set to application/x-www-form-urlencoded.

When err is nil, resp always contains a non-nil resp.Body. Caller should close resp.Body when done reading from it.

func PostFormFiles

func PostFormFiles(url string, data url.Values, files []*UploadFile, opts ...RequestOption) (resp *http.Response, err error)

PostFormFiles issues a POST to the specified URL, with data's keys and values URL-encoded as the request body, and files as multipart form.

The Content-Type header is set to multipart/form-data.

When err is nil, resp always contains a non-nil resp.Body. Caller should close resp.Body when done reading from it.

func PostJSON

func PostJSON(url string, body any, opts ...RequestOption) (resp *http.Response, err error)

PostJSON issues a POST to the specified URL with the given body as JSON. values JSON as the request body.

The Content-Type header is set to application/json.

When err is nil, resp always contains a non-nil resp.Body. Caller should close resp.Body when done reading from it.

func Put

func Put(url string, contentType string, body io.Reader, opts ...RequestOption) (resp *http.Response, err error)

Put issues a PUT to the specified URL.

When err is nil, resp always contains a non-nil resp.Body. Caller should close resp.Body when done reading from it.

func PutJSON

func PutJSON(url string, body any, opts ...RequestOption) (resp *http.Response, err error)

PutJSON issues a PUT to the specified URL with the given body as JSON. values JSON as the request body.

The Content-Type header is set to application/json.

When err is nil, resp always contains a non-nil resp.Body. Caller should close resp.Body when done reading from it.

func ReadAll

func ReadAll(resp *http.Response) (b []byte, err error)

ReadAll read all data from resp

func ReadAnyJSON

func ReadAnyJSON(resp *http.Response, v any) (err error)

ReadAnyJSON read json from resp.Body

func ReadJSON

func ReadJSON[T any](resp *http.Response) (T, error)

ReadJSON read json from resp.Body

func ReadString

func ReadString(resp *http.Response) (s string, err error)

ReadString read string from resp.Body and auto convert encoding

func SaveFile

func SaveFile(resp *http.Response, name string) (written int64, err error)

SaveFile save file from resp.Body

func WithContentType

func WithContentType(contentType string) func(*RequestOptions)

WithContentType If a content type is set, each HTTP request will use this content type to make the request.

func WithDialTimeout

func WithDialTimeout(dialTimeout time.Duration) func(*ClientOptions)

WithDialTimeout If a dial timeout is set, each HTTP request will use this time as the maximum limit for establishing a connection.

func WithHeaders

func WithHeaders(headers http.Header) func(*RequestOptions)

WithHeaders If headers are set, each HTTP request will use these headers to make the request.

func WithKeepAliveTimeout

func WithKeepAliveTimeout(keepAliveTimeout time.Duration) func(*ClientOptions)

WithKeepAliveTimeout If a keep-alive timeout is set, each HTTP request will use this time as the maximum limit for keeping the connection alive.

func WithProxy

func WithProxy(proxy string) func(*ClientOptions)

WithProxy If a proxy is set, each HTTP request will use this proxy to make the request.

func WithReferer

func WithReferer(referer string) func(*RequestOptions)

WithReferer If a referer is set, each HTTP request will use this referer to make the request.

func WithTLSHandshakeTimeout

func WithTLSHandshakeTimeout(tlsHandshakeTimeout time.Duration) func(*ClientOptions)

WithTLSHandshakeTimeout If a TLS handshake timeout is set, each HTTP request will use this time as the maximum limit for completing the TLS handshake.

func WithUserAgent

func WithUserAgent(userAgent string) func(*ClientOptions)

WithUserAgent sed to replace the default UserAgent when making a request.

Types

type Client

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

func NewClient

func NewClient(ctx context.Context, opts ...ClientOption) *Client

NewClient new client

func (*Client) Close

func (c *Client) Close()

Close close

func (*Client) Delete

func (c *Client) Delete(url string, opts ...RequestOption) (resp *http.Response, err error)

Delete issues a DELETE to the specified URL.

When err is nil, resp always contains a non-nil resp.Body. Caller should close resp.Body when done reading from it.

func (*Client) Do

func (c *Client) Do(req *http.Request, opts ...RequestOption) (resp *http.Response, err error)

Do sends an HTTP request and returns an HTTP response, following policy (such as redirects, cookies, auth) as configured on the client.

An error is returned if caused by client policy (such as CheckRedirect), or failure to speak HTTP (such as a network connectivity problem). A non-2xx status code doesn't cause an error.

If the returned error is nil, the [Response] will contain a non-nil Body which the user is expected to close. If the Body is not both read to EOF and closed, the Client's underlying [RoundTripper] (typically [Transport]) may not be able to re-use a persistent TCP connection to the server for a subsequent "keep-alive" request.

The request Body, if non-nil, will be closed by the underlying Transport, even on errors. The Body may be closed asynchronously after Do returns.

On error, any Response can be ignored. A non-nil Response with a non-nil error only occurs when CheckRedirect fails, and even then the returned [Response.Body] is already closed.

Generally Get, Post, or PostForm will be used instead of Do.

If the server replies with a redirect, the Client first uses the CheckRedirect function to determine whether the redirect should be followed. If permitted, a 301, 302, or 303 redirect causes subsequent requests to use HTTP method GET (or HEAD if the original request was HEAD), with no body. A 307 or 308 redirect preserves the original HTTP method and body, provided that the [Request.GetBody] function is defined. The [NewRequest] function automatically sets GetBody for common standard library body types.

Any returned error will be of type *url.Error. The url.Error value's Timeout method will report true if the request timed out.

func (*Client) Get

func (c *Client) Get(url string, opts ...RequestOption) (resp *http.Response, err error)

Get issues a GET to the specified URL.

When err is nil, resp always contains a non-nil resp.Body. Caller should close resp.Body when done reading from it.

func (*Client) Head

func (c *Client) Head(url string, opts ...RequestOption) (resp *http.Response, err error)

Head issues a HEAD to the specified URL.

When err is nil, resp always contains a non-nil resp.Body. Caller should close resp.Body when done reading from it.

func (*Client) Patch

func (c *Client) Patch(url string, contentType string, body io.Reader, opts ...RequestOption) (resp *http.Response, err error)

Patch issues a PATCH to the specified URL.

When err is nil, resp always contains a non-nil resp.Body. Caller should close resp.Body when done reading from it.

func (*Client) PatchJSON

func (c *Client) PatchJSON(url string, body any, opts ...RequestOption) (resp *http.Response, err error)

PatchJSON issues a PATCH to the specified URL with the given body as JSON. values JSON as the request body.

The Content-Type header is set to application/json.

When err is nil, resp always contains a non-nil resp.Body. Caller should close resp.Body when done reading from it.

func (*Client) Post

func (c *Client) Post(url string, contentType string, body io.Reader, opts ...RequestOption) (resp *http.Response, err error)

Post issues a POST to the specified URL.

When err is nil, resp always contains a non-nil resp.Body. Caller should close resp.Body when done reading from it.

func (*Client) PostForm

func (c *Client) PostForm(url string, data url.Values, opts ...RequestOption) (resp *http.Response, err error)

PostForm issues a POST to the specified URL, with data's keys and values URL-encoded as the request body.

The Content-Type header is set to application/x-www-form-urlencoded.

When err is nil, resp always contains a non-nil resp.Body. Caller should close resp.Body when done reading from it.

func (*Client) PostFormFiles

func (c *Client) PostFormFiles(url string, data url.Values, files []*UploadFile, opts ...RequestOption) (resp *http.Response, err error)

PostFormFiles issues a POST to the specified URL, with data's keys and values URL-encoded as the request body, and files as multipart form.

The Content-Type header is set to multipart/form-data.

When err is nil, resp always contains a non-nil resp.Body. Caller should close resp.Body when done reading from it.

func (*Client) PostJSON

func (c *Client) PostJSON(url string, body any, opts ...RequestOption) (resp *http.Response, err error)

PostJSON issues a POST to the specified URL with the given body as JSON. values JSON as the request body.

The Content-Type header is set to application/json.

When err is nil, resp always contains a non-nil resp.Body. Caller should close resp.Body when done reading from it.

func (*Client) Put

func (c *Client) Put(url string, contentType string, body io.Reader, opts ...RequestOption) (resp *http.Response, err error)

Put issues a PUT to the specified URL.

When err is nil, resp always contains a non-nil resp.Body. Caller should close resp.Body when done reading from it.

func (*Client) PutJSON

func (c *Client) PutJSON(url string, body any, opts ...RequestOption) (resp *http.Response, err error)

PutJSON issues a PUT to the specified URL with the given body as JSON. values JSON as the request body.

The Content-Type header is set to application/json.

When err is nil, resp always contains a non-nil resp.Body. Caller should close resp.Body when done reading from it.

type ClientOption

type ClientOption func(*ClientOptions)

ClientOption http client option

type ClientOptions

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

ClientOptions http client options

type RequestOption

type RequestOption func(*RequestOptions)

RequestOption http request option

type RequestOptions

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

RequestOptions http request options

type UploadFile

type UploadFile struct {
	FieldName string
	FileName  string
	Body      io.Reader
}

UploadFile upload file struct

Jump to

Keyboard shortcuts

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