Documentation ¶
Overview ¶
Package remote provides a simple and clean API for making HTTP requests to remote servers.
Package remote provides a simple and clean API for making HTTP requests to remote servers.
Index ¶
- Constants
- type ErrorReport
- type Option
- type Transaction
- func (t *Transaction) Accept(contentTypes ...string) *Transaction
- func (t *Transaction) Body(value string) *Transaction
- func (t *Transaction) ContentType(value string) *Transaction
- func (t *Transaction) Error(object any) *Transaction
- func (t *Transaction) Form(name string, value string) *Transaction
- func (t *Transaction) Header(name string, value string) *Transaction
- func (t *Transaction) JSON(value any) *Transaction
- func (t *Transaction) Query(name string, value string) *Transaction
- func (t *Transaction) RequestBody() ([]byte, error)
- func (t *Transaction) RequestURL() string
- func (t *Transaction) Response() *http.Response
- func (t *Transaction) ResponseBody() ([]byte, error)
- func (t *Transaction) ResponseBodyReader() io.Reader
- func (t *Transaction) ResponseContentType() string
- func (t *Transaction) ResponseHeader() http.Header
- func (t *Transaction) ResponseStatusCode() int
- func (t *Transaction) Result(object any) *Transaction
- func (t *Transaction) Send() error
- func (t *Transaction) Use(options ...Option) *Transaction
- func (t *Transaction) UserAgent(value string) *Transaction
- func (t *Transaction) WithOptions(options ...Option) *Transaction
- func (t *Transaction) XML(value any) *Transaction
Constants ¶
const Accept = "Accept"
Accept is the string used in the HTTP header to request a response be encoded as a MIME type
const ContentType = "Content-Type"
ContentType is the string used in the HTTP header to designate a MIME type
const ContentTypeActivityPub = "application/activity+json"
ContentTypeActivityPub is the standard MIME type for ActivityPub content
const ContentTypeAtomXML = "application/atom+xml"
const ContentTypeForm = "application/x-www-form-urlencoded"
ContentTypeForm is the standard MIME Type for Form encoded content
const ContentTypeHTML = "text/html"
ContentTypeHTML is the standard MIME type for HTML content
const ContentTypeJSON = "application/json"
ContentTypeJSON is the standard MIME Type for JSON content
const ContentTypeJSONFeed = "application/feed+json"
ContentTypeJSONFeed is the standard MIME Type for JSON Feed content https://en.wikipedia.org/wiki/JSON_Feed
const ContentTypeJSONLD = "application/ld+json"
ContentTypeJSONLD is the standard MIME Type for JSON-LD content https://en.wikipedia.org/wiki/JSON-LD
const ContentTypeJSONResourceDescriptor = "application/jrd+json"
ContentTypeJSONResourceDescriptor is the standard MIME Type for JSON Resource Descriptor content which is used by WebFinger: https://datatracker.ietf.org/doc/html/rfc7033#section-10.2
const ContentTypePlain = "text/plain"
ContentTypePlain is the default plaintext MIME type
const ContentTypeRSSXML = "application/rss+xml"
const ContentTypeXML = "application/xml"
ContentTypeXML is the standard MIME Type for XML content
const UserAgent = "User-Agent"
UserAgent is the string used in the HTTP header to identify the client making the request
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type ErrorReport ¶
type ErrorReport struct { URL string `json:"url"` Request struct { Method string `json:"method"` Header map[string]string `json:"header"` Body string `json:"body"` } `json:"request"` Response struct { StatusCode int `json:"statusCode"` Status string `json:"status"` Header http.Header `json:"header"` Body string `json:"body"` } `json:"response"` }
ErrorReport includes all the data returned by a transaction if it throws an error for any reason.
type Option ¶ added in v0.12.0
type Option struct { // BeforeRequest is called before an http.Request is generated. It can be used to // modify Transaction values before they are assembled into an http.Request object. BeforeRequest func(*Transaction) error // ModifyRequest is called after an http.Request has been generated, but before it is sent to the // remote server. It can be used to modify the request, or to replace it entirely. // If it returns a non-nil http.Response, then that is used INSTEAD OF calling the remote server. // If it returns a nil http.Response, then the request is sent to the remote server as normal. ModifyRequest func(*Transaction, *http.Request) *http.Response // AfterRequest is executed after an http.Response has been received from the remote server, but before it is // parsed and returned to the calling application. AfterRequest func(*Transaction, *http.Response) error }
Option is a decorator that can modify the request before it is sent to the remote HTTP server, or modify the response after it is returned by the remote HTTP server.
type Transaction ¶
type Transaction struct {
// contains filtered or unexported fields
}
Transaction represents a single HTTP request/response to a remote HTTP server.
func Delete ¶
func Delete(url string) *Transaction
Delete creates a new HTTP request to the designated URL, using the DELETE method.
func Get ¶
func Get(url string) *Transaction
Get creates a new HTTP request to the designated URL, using the GET method
func Patch ¶
func Patch(url string) *Transaction
Patch creates a new HTTP request to the designated URL, using the PATCH method
func Post ¶
func Post(url string) *Transaction
Post creates a new HTTP request to the designated URL, using the POST method
func Put ¶
func Put(url string) *Transaction
Put creates a new HTTP request to the designated URL, using the PUT method
func (*Transaction) Accept ¶ added in v0.10.1
func (t *Transaction) Accept(contentTypes ...string) *Transaction
Accept sets the Content-Type header of the HTTP request.
func (*Transaction) Body ¶
func (t *Transaction) Body(value string) *Transaction
Body sets the request body, to be encoded as plain text
func (*Transaction) ContentType ¶
func (t *Transaction) ContentType(value string) *Transaction
ContentType sets the Content-Type header of the HTTP request.
func (*Transaction) Error ¶ added in v0.12.0
func (t *Transaction) Error(object any) *Transaction
Error sets the object for parsing HTTP error responses
func (*Transaction) Form ¶
func (t *Transaction) Form(name string, value string) *Transaction
Form adds a name/value pair to the form data to be sent to the remote server.
func (*Transaction) Header ¶
func (t *Transaction) Header(name string, value string) *Transaction
Header sets a designated header value in the HTTP request.
func (*Transaction) JSON ¶
func (t *Transaction) JSON(value any) *Transaction
JSON sets the request body, to be encoded as JSON.
func (*Transaction) Query ¶
func (t *Transaction) Query(name string, value string) *Transaction
Query sets a name/value pair in the URL query string.
func (*Transaction) RequestBody ¶ added in v0.12.0
func (t *Transaction) RequestBody() ([]byte, error)
RequestBody returns the serialized body of the request as a slice of bytes.
func (*Transaction) RequestURL ¶ added in v0.12.0
func (t *Transaction) RequestURL() string
func (*Transaction) Response ¶
func (t *Transaction) Response() *http.Response
Response returns the original HTTP response object.
func (*Transaction) ResponseBody ¶ added in v0.12.0
func (t *Transaction) ResponseBody() ([]byte, error)
ResponseBytes returns the original response body, as a byte array. This method replaces the original body reader, meaning that it can be called multiple times without error.
func (*Transaction) ResponseBodyReader ¶ added in v0.12.0
func (t *Transaction) ResponseBodyReader() io.Reader
func (*Transaction) ResponseContentType ¶ added in v0.12.0
func (t *Transaction) ResponseContentType() string
ResponseContentType returns the Content-Type header of the response.
func (*Transaction) ResponseHeader ¶ added in v0.12.0
func (t *Transaction) ResponseHeader() http.Header
ResponseHeader returns the HTTP response header.
func (*Transaction) ResponseStatusCode ¶ added in v0.12.0
func (t *Transaction) ResponseStatusCode() int
ResponseStatusCode returns the HTTP status code of the response.
func (*Transaction) Result ¶ added in v0.12.0
func (t *Transaction) Result(object any) *Transaction
Result sets the object for parsing HTTP success responses
func (*Transaction) Send ¶
func (t *Transaction) Send() error
Send executes the transaction, sending the request to the remote server.
func (*Transaction) Use ¶
func (t *Transaction) Use(options ...Option) *Transaction
Use lets you add remote.Options to the transaction. Options modify transaction data before and after it is sent to the remote server.
func (*Transaction) UserAgent ¶ added in v0.12.0
func (t *Transaction) UserAgent(value string) *Transaction
func (*Transaction) WithOptions ¶ added in v0.12.0
func (t *Transaction) WithOptions(options ...Option) *Transaction
WithOptions is an alias for the `Use` method. It applies one or more request.options to the Transaction.
func (*Transaction) XML ¶
func (t *Transaction) XML(value any) *Transaction
XML sets the request body, to be encoded as XML.