requests

package module
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Sep 11, 2021 License: MIT Imports: 7 Imported by: 0

README

requests

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

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)

}

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

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

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) 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) 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 of the header and whether it exists.

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) 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 before setting (converts to lowercase).

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 (resp *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 (resp *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