Documentation ¶
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type HTTPMethod ¶
type HTTPMethod int
HTTPMethod is a type that represents an HTTP request method. Read more here: https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods
const ( GET HTTPMethod = iota // An HTTP GET method POST // An HTTP POST method PUT // An HTTP PUT method DELETE // An HTTP DELETE method OPTIONS // An HTTP OPTIONS method HEAD // An HTTP HEAD method CONNECT // An HTTP CONNECT method TRACE // An HTTP TRACE method PATCH // An HTTP PATCH method )
Enums representing HTTP methods
func (HTTPMethod) String ¶
func (m HTTPMethod) String() string
Convert an HTTPMethod to it's string format
type Request ¶
type Request struct { URL string // URL to send the request to Method HTTPMethod // HTTP method to use Headers map[string]string // Headers to send with the request Body []byte // Body to send with the request Timeout time.Duration // Timeout for the request }
Request is a type that represents an HTTP request
func NewGetRequest ¶ added in v0.1.0
NewGetRequest creates a new Request object with the supplied URL and sets the HTTP method to GET.
func NewPostRequest ¶ added in v0.1.0
NewPostRequest creates a new Request object with the supplied URL, content-type header, and body sets the HTTP method to POST.
func (*Request) DelHeader ¶
DelHeader deletes a header value from the request headers if it exists. Normalizes the key to lowercase before deleting.
func (*Request) GetHeader ¶
GetHeader gets a header value from the request. Normalizes the key to lowercase before checking. Returns the value of the header and whether it exists.
func (*Request) MustSend ¶
MustSend sends the HTTP request and panic if an error is returned. (Calls Send() internally)
func (*Request) Send ¶
Send sends the HTTP request with the supplied parameters
Example ¶
package main import ( "fmt" "github.com/a-poor/requests" ) func main() { r := requests.Request{ Method: requests.GET, URL: "http://example.com", } res, err := r.Send() if err != nil { // handle error } fmt.Println(res.StatusCode) }
Output: 200
type Response ¶
type Response struct { Ok bool // Was the request successful? (Status codes: 200-399) StatusCode int // HTTP response status code Headers map[string]string // HTTP Response headers Body []byte // HTTP Response body }
Response is a type that represents an HTTP response returned from an HTTP request
func SendGetRequest ¶ added in v0.1.0
SendGetRequest creates a new HTTP GET request and sends it to the specified URL. Internally, calls `NewGetRequest(url).Send()`
Example ¶
package main import ( "fmt" "github.com/a-poor/requests" ) func main() { res, err := requests.SendGetRequest("http://example.com") if err != nil { // handle error } fmt.Println(res.StatusCode) }
Output: 200
func SendPostRequest ¶ added in v0.1.0
SendPostRequest creates a new HTTP POST request and sends it to the specified URL. Internally, calls `NewPostRequest(url, contentType, body).Send()`