winhttp

package
v0.22.4 Latest Latest
Warning

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

Go to latest
Published: May 7, 2024 License: GPL-3.0 Imports: 11 Imported by: 0

README

WinHTTP

Usage

Minimal example:

package main

import (
    "bytes"
    "crypto/tls"
    "fmt"
    "io"
    "net/http"
    "net/http/cookiejar"

    "github.com/mjwhitta/win/winhttp"
)

func main() {
    var body io.Reader = bytes.NewReader([]byte("test"))
    var dst string = "http://127.0.0.1:8080/asdf"
    var e error
    var req *http.Request
    var res *http.Response

    if t, ok := http.DefaultTransport.(*http.Transport); ok {
        if t.TLSClientConfig == nil {
            t.TLSClientConfig = &tls.Config{}
        }

        t.TLSClientConfig.InsecureSkipVerify = true
    }

    if winhttp.DefaultClient.Jar, e = cookiejar.New(nil); e != nil {
        panic(e)
    }

    if res, e = winhttp.Get(dst); e != nil {
        panic(e)
    }

    if e = output(res); e != nil {
        panic(e)
    }

    req, e = http.NewRequest(http.MethodPost, dst, body)
    if e != nil {
        panic(e)
    }

    req.AddCookie(&http.Cookie{Name: "chocolatechip", Value: "tasty"})
    req.AddCookie(&http.Cookie{Name: "oatmealraisin", Value: "gross"})
    req.AddCookie(&http.Cookie{Name: "snickerdoodle", Value: "yummy"})
    req.Header.Set("User-Agent", "testing, testing, 1, 2, 3...")

    if res, e = winhttp.DefaultClient.Do(req); e != nil {
        panic(e)
    }
    defer res.Body.Close()

    if e = output(res); e != nil {
        panic(e)
    }
}

func output(res *http.Response) error {
    var b []byte
    var e error

    if b, e = io.ReadAll(res.Body); e != nil {
        return e
    }

    fmt.Println(res.Status)

    for k := range res.Header {
        fmt.Printf("%s: %s\n", k, res.Header.Get(k))
    }

    for _, cookie := range res.Cookies() {
        fmt.Printf("%s = %s\n", cookie.Name, cookie.Value)
    }

    if len(b) > 0 {
        fmt.Println(string(b))
    }

    fmt.Println()

    return nil
}

Documentation

Rendered for windows/amd64

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Get added in v0.11.0

func Get(url string) (*http.Response, error)

Get will make a GET request using the DefaultClient.

func Head(url string) (*http.Response, error)

Head will make a HEAD request using the DefaultClient.

func Post added in v0.11.0

func Post(
	url string, contentType string, body io.Reader,
) (*http.Response, error)

Post will make a POST request using the DefaultClient.

func PostForm added in v0.20.0

func PostForm(url string, data url.Values) (*http.Response, error)

PostForm will make a POST request using the DefaultClient.

Types

type Client added in v0.11.0

type Client struct {
	Debug     bool
	Jar       http.CookieJar
	Timeout   time.Duration
	Transport *http.Transport
	// contains filtered or unexported fields
}

Client is a struct containing relevant metadata to make HTTP requests.

var DefaultClient *Client

DefaultClient is the default client similar to net/http.

func NewClient added in v0.11.0

func NewClient(ua ...string) (*Client, error)

NewClient will return a pointer to a new Client instance that simply wraps the net/http.Client type.

func (*Client) Do added in v0.11.0

func (c *Client) Do(req *http.Request) (*http.Response, error)

Do will send the HTTP request and return an HTTP response.

func (*Client) Get added in v0.11.0

func (c *Client) Get(url string) (*http.Response, error)

Get will make a GET request using WinHTTP.dll.

func (*Client) Head added in v0.11.0

func (c *Client) Head(url string) (*http.Response, error)

Head will make a HEAD request using WinHTTP.dll.

func (*Client) Post added in v0.11.0

func (c *Client) Post(
	url string, contentType string, body io.Reader,
) (*http.Response, error)

Post will make a POST request using WinHTTP.dll.

func (*Client) PostForm added in v0.20.0

func (c *Client) PostForm(
	url string, data url.Values,
) (*http.Response, error)

PostForm will make a POST request using WinHTTP.dll.

Jump to

Keyboard shortcuts

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