request

package module
v0.0.0-...-2c9eb5d Latest Latest
Warning

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

Go to latest
Published: Jul 19, 2024 License: MIT Imports: 7 Imported by: 0

README

Request

A library to send HTTP requests and read the response.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func SendParse

func SendParse[T any](ctx context.Context, client *http.Client, r Request, acceptable ...int) (*T, error)

SendParse is intended for use cases which caller is sure about the response structure. Optionally, caller can provide a number of acceptable status codes. Function will return an error if the response's status code is not in them.

This function requires the caller to specify the response type. Return value will be a pointer of that type, or an error if something goes wrong.

Types

type Method

type Method = string

Method defines all HTTP verbs.

const (
	GET     Method = "GET"
	POST    Method = "POST"
	PUT     Method = "PUT"
	PATCH   Method = "PATCH"
	DELETE  Method = "DELETE"
	HEAD    Method = "HEAD"
	OPTIONS Method = "OPTIONS"
	TRACE   Method = "TRACE"
	CONNECT Method = "CONNECT"
)

type Request

type Request struct {
	Method  Method
	URL     string
	Header  http.Header
	Cookies []*http.Cookie
	Body    []byte
	Params  map[string]string
}

Request includes all the necessary data for creating an HTTP request. Method can be a string; or be one of the predefined ones by this module. URL must be the full address with all the prefix and suffixes. Header, Cookies and Body are not mandatory and might be filled based on the requirements. Params is a map for providing URL-encoded query parameters.

type Response

type Response struct {
	Body       []byte
	Header     http.Header
	Cookies    []*http.Cookie
	StatusCode int
}

Response consists of some of the HTTP response data.

func Send

func Send(ctx context.Context, client *http.Client, r Request) (*Response, error)

Send sends an HTTP request based on the Request. It uses the provided http.Client in order to reuse the client. It returns Response if successful, or an error otherwise.

Jump to

Keyboard shortcuts

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