requests

package module
v0.6.0 Latest Latest
Warning

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

Go to latest
Published: Sep 25, 2021 License: MIT Imports: 9 Imported by: 0

README

requests

Go Reference GitHub go.mod Go version Go Test Go Report Card GitHub GitHub last commit Sourcegraph CodeFactor

created by Austin Poor

A quick and easy HTTP request library written in Go.

Installation

go get github.com/a-poor/requests

Quick Start

package main

import (
    "fmt"
    "github.com/a-poor/requests"
)

func main() {
    // Send the request
    res, err := requests.SendGetRequest("https://google.com")

    // If there was an error, print and return
    if err != nil {
        fmt.Printf("Error: %e\n", err)
        return
    }

    // Print the response's status code
    fmt.Printf("Status Code: %d\n", res.StatusCode)

}

ToDo

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func JSONMust added in v0.3.0

func JSONMust(data map[string]interface{}) []byte

JSONMust marshals a map into a JSON byte slice using json.Marshal and panics if there is an error.

func URLEncode added in v0.6.0

func URLEncode(data interface{}) string

URLEncode will encode `data` as a URL-safe string Uses fmt.Sprint and url.PathEscape to do the encoding.

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
	Query   map[string]string // Query parameters 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

Notes:

  • Headers and Query dont support multiple values
  • Timeout of 0 means no timeout

func NewGetRequest added in v0.1.0

func NewGetRequest(url string) *Request

NewGetRequest creates a new Request object with the supplied URL and sets the HTTP method to GET.

func NewPostRequest added in v0.1.0

func NewPostRequest(url string, contentType string, body []byte) *Request

NewPostRequest creates a new Request object with the supplied URL, content-type header, and body sets the HTTP method to POST.

func (*Request) Copy added in v0.6.0

func (req *Request) Copy() *Request

Copy will create a copy of the Request object

func (*Request) DelHeader

func (req *Request) DelHeader(name string)

DelHeader deletes a header value from the request headers if it exists. Normalizes the key to lowercase before deleting.

func (*Request) DelQuery added in v0.5.0

func (req *Request) DelQuery(name string)

DelQuery deletes a query value from the request headers if it exists.

func (*Request) GetHeader

func (req *Request) GetHeader(name string) (string, bool)

GetHeader gets a header value from the request. Normalizes the key to lowercase before checking. Returns the value associated with the key and whether it exists.

func (*Request) GetQuery added in v0.5.0

func (req *Request) GetQuery(name string) (string, bool)

GetQuery gets a query value from the request. Returns the value associated with the key and whether it exists.

func (*Request) MustParsePathParams added in v0.6.0

func (req *Request) MustParsePathParams(data interface{}) *Request

MustParsePathParams is the same as ParsePathParams except it panics if there is an error.

func (*Request) MustSend

func (req *Request) MustSend() *Response

MustSend sends the HTTP request and panic if an error is returned. (Calls Send() internally)

func (*Request) ParsePathParams added in v0.6.0

func (req *Request) ParsePathParams(data interface{}) (*Request, error)

ParsePathParams will create a copy of the Request object and replace URL parameters with the supplied data.

Note: The URL template has access to the `URLEncode` function which can be used to safely encode a string. (ex `{{ "Hello world" | URLEncode }}` will return `Hello%20world`)

func (*Request) Send

func (req *Request) Send() (*Response, error)

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

func (*Request) SetHeader

func (req *Request) SetHeader(name, value string)

SetHeader sets a header value in the request. Normalizes the key to lowercase before setting.

func (*Request) SetQuery added in v0.5.0

func (req *Request) SetQuery(name, value string)

SetQuery sets a header value in the request.

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

func SendGetRequest(url string) (*Response, error)

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

func SendPostRequest(url string, contentType string, body []byte) (*Response, error)

SendPostRequest creates a new HTTP POST request and sends it to the specified URL. Internally, calls `NewPostRequest(url, contentType, body).Send()`

func (*Response) GetHeader

func (res *Response) GetHeader(name string) (string, bool)

GetHeader gets a header value from the response if it exists. Normalizes the key to lowercase before checking. Returns the value of the header and whether it exists.

func (*Response) JSON

func (res *Response) JSON() (map[string]interface{}, error)

JSON unmarshalls the response body into a map

Jump to

Keyboard shortcuts

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