requests

package module
v0.3.8 Latest Latest
Warning

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

Go to latest
Published: Sep 5, 2021 License: MIT Imports: 13 Imported by: 5

README

Requests

A golang library for avoid headache during HTTP request

Codacy Badge Go Report Card GoDoc License Version Code size Repo size Issue open Issue closed

Example usage

Requests can work in two different method:

  • Single request
  • Multiple request in parallel
Single Request

In order to make a Request, you need to initialize the client:

// Initialize request
var req requests.Request

Now the request is ready to be populated with headers or body-data if necessary.

You can create a list of headers and explode the data into the delegated method. In alternative, you can pass a given number of headers to the method.
NOTE: The headers have to be a "key:value" list so the first argument will be the key of the header, the one after will be the value.

//Set custom headers directly in the method
req.CreateHeaderList("Content-Type", "text/plain; charset=UTF-8", "Authorization", "Basic cG9zdG1hbjpwYXNzd29yZA==")
// Or create a list of headers and use them in the method
var headers []string
headers = append(headers, "Content-Type")
headers = append(headers, "text/plain; charset=UTF-8")
headers = append(headers, "Authorization")
headers = append(headers, "Basic cG9zdG1hbjpwYXNzd29yZA==")
req.CreateHeaderList(headers...)

Now you can send a request to the URL

// Send the request and save to a properly structure
// GET, without BODY data (only used in POST), and enabling SSL certificate validation (skipTLS: false)
response := req.SendRequest("https://postman-echo.com/get?foo1=bar1&foo2=bar2", "GET", nil, false)

// Debug the response
fmt.Println("Headers: ", response.Headers)
fmt.Println("Status code: ", response.StatusCode)
fmt.Println("Time elapsed: ", response.Time)
fmt.Println("Error: ", response.Error)
fmt.Println("Body: ", string(response.Body))
Multiple request in parallel

In order to use the parallel request, you have to create a list of request that have to be executed, than you can call the delegated method for send them in parallel, choosing how many requests have to be sent in parallel.

In first instance populate an array with the requests that you need to send:

// This array will contains the list of request
var reqs []requests.Request

// N is the number of request to run in parallel, in order to avoid "TO MANY OPEN FILES"
var N int = 12

// Create the list of request
for i := 0; i < 1000; i++ {
    // Example python server, you can find under the "example" folder
    req, err := requests.InitRequest("https://127.0.0.1:5000", "GET", nil, nil, i%2 == 0) // Alternate cert validation
    if err != nil {
        log.Println("Skipping request [", i, "]. Error: ", err)
    } else {
        // If no error, we can append the request created to the list of request that we need to send
        reqs = append(reqs, *req)
    }
}
// This array will contains the response from the given request
var response []datastructure.Response

// send the request using N request to send in parallel
response = requests.ParallelRequest(reqs, N)

// Print the response
for i := range response {
    log.Println("Request [", i, "] -> ", response[i].Dump())
}

More example

Please, refer to the example code

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ParallelRequest added in v0.2.3

func ParallelRequest(reqs []Request, N int) []datastructure.Response

ParallelRequest is delegated to run the given list of request in parallel, sending N request at each time

Types

type Request added in v0.2.3

type Request struct {
	Req     *http.Request          // Request
	Tr      *http.Transport        // Transport layer, used for enable/disable TLS verification
	Method  string                 // HTTP method of the request
	URL     string                 // URL where send the request
	Data    []byte                 // BODY in case of POST, ARGS in case of GET
	Resp    datastructure.Response // Struct for save the response
	Timeout time.Duration          // Timeout of the request
}

Request will contain all the data related to the current HTTP request and response.

func InitDebugRequest added in v0.2.3

func InitDebugRequest() Request

InitDebugRequest is delegated to set the log level in order to debug the flow

func InitRequest added in v0.2.3

func InitRequest(url, method string, bodyData []byte, skipTLS, debug bool) (*Request, error)

InitRequest is delegated to initialize a new request with the given parameter. NOTE: it will use the default timeout -> NO TIMEOUT. In order to specify a different timeout you can use the delegated method NOTE: headers have to be set with the delegated method

func (*Request) AddCookie added in v0.2.4

func (req *Request) AddCookie(c ...*http.Cookie) error

AddCookie is delegated to add the given list of cookie to the request

func (*Request) AddHeader added in v0.3.6

func (req *Request) AddHeader(key, value string) error

AddHeader is delegated to add a new header to the request

func (*Request) CreateHeaderList added in v0.2.3

func (req *Request) CreateHeaderList(headers ...string) error

CreateHeaderList is delegated to initialize a list of headers. Every row of the matrix contains [key,value]

func (*Request) ExecuteRequest added in v0.2.3

func (req *Request) ExecuteRequest(client *http.Client) datastructure.Response

ExecuteRequest is delegated to run a previously allocated request.

func (*Request) SendRequest added in v0.2.3

func (req *Request) SendRequest(url, method string, bodyData []byte, headers []string, skipTLS bool, timeout time.Duration) *datastructure.Response

SendRequest is delegated to initialize a new HTTP request.

func (*Request) SetBasicAuth added in v0.3.5

func (req *Request) SetBasicAuth(username, password string)

SetBasicAuth is delegated to compute the Basic Authentication value for the given data

func (*Request) SetBearerAuth added in v0.3.5

func (req *Request) SetBearerAuth(token string) error

SetBearerAuth is delegated to compute the Bearer token for the given data

func (*Request) SetTLS added in v0.2.3

func (req *Request) SetTLS(skipTLS bool)

SetTLS is delegated to enable/disable TLS certificate validation

func (*Request) SetTimeout added in v0.2.3

func (req *Request) SetTimeout(t time.Duration)

SetTimeout is delegated to validate the given timeout and set to the request

func (*Request) SetTransportLayer added in v0.3.7

func (req *Request) SetTransportLayer(tl *http.Transport)

Directories

Path Synopsis
A go webserver for benchmark the request
A go webserver for benchmark the request

Jump to

Keyboard shortcuts

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