requestbuilder

package
v0.0.0-...-9c153ef Latest Latest
Warning

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

Go to latest
Published: May 3, 2024 License: Apache-2.0 Imports: 4 Imported by: 0

Documentation

Overview

Package requestbuilder contains a builder using httptest.NewRequest with methods supporting invocation chaining.

Example
package main

import (
	"fmt"
	"os"

	"github.com/halimath/httputils/requestbuilder"
)

func main() {
	accessToken := "..."
	data, _ := os.Open("/some/file")

	_ = requestbuilder.Post("https://example.com/path/to/resource").
		Body(data).
		AddHeader("Authorization", fmt.Sprintf("Bearer %s", accessToken)).
		Request()
}
Output:

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type RequestBuilder

type RequestBuilder struct {
	// contains filtered or unexported fields
}

RequestBuilder implements a builder for http.Request values using httptest.NewRequest internally.

func Delete

func Delete(target string) *RequestBuilder

Delete creates a new builder using HTTP verb DELETE. target is used the same way as described for httptest.NewRequest.

func Get

func Get(target string) *RequestBuilder

Get creates a new builder using HTTP verb GET. target is used the same way as described for httptest.NewRequest.

func Head(target string) *RequestBuilder

Head creates a new builder using HTTP verb HEAD. target is used the same way as described for httptest.NewRequest.

func New

func New(method string, target string) *RequestBuilder

New creates a new request builder with method and target. target is used the same way as described for httptest.NewRequest.

func Options

func Options(target string) *RequestBuilder

Options creates a new builder using HTTP verb OPTIONS. target is used the same way as described for httptest.NewRequest.

func Patch

func Patch(target string) *RequestBuilder

Patch creates a new builder using HTTP verb PATCH. target is used the same way as described for httptest.NewRequest.

func Post

func Post(target string) *RequestBuilder

Post creates a new builder using HTTP verb POST. target is used the same way as described for httptest.NewRequest.

func Put

func Put(target string) *RequestBuilder

Put creates a new builder using HTTP verb PUT. target is used the same way as described for httptest.NewRequest.

func Trace

func Trace(target string) *RequestBuilder

Trace creates a new builder using HTTP verb TRACE. target is used the same way as described for httptest.NewRequest.

func (*RequestBuilder) AddHeader

func (r *RequestBuilder) AddHeader(key, value string) *RequestBuilder

AddHeader adds a request header and returns the builder.

func (*RequestBuilder) AddQueryParam

func (r *RequestBuilder) AddQueryParam(key, value string) *RequestBuilder

AddQueryParam adds a URL query parameter and returns the builder.

func (*RequestBuilder) Body

func (r *RequestBuilder) Body(body io.Reader) *RequestBuilder

Body sets the request's body and returns the builder.

func (*RequestBuilder) Request

func (r *RequestBuilder) Request() *http.Request

Request creates a new http.Request using httptest.NewRequest internally. After the request has been created the builder may be used to build more requests including requests with modified values.

Jump to

Keyboard shortcuts

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