or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

bpf.mdcontext-ctxhttp.mdcontext.mddict.mddns-dnsmessage.mdhtml-atom.mdhtml-charset.mdhtml.mdhttp-httpguts.mdhttp-httpproxy.mdhttp2-h2c.mdhttp2-hpack.mdhttp2.mdicmp.mdidna.mdindex.mdipv4.mdipv6.mdnettest.mdnetutil.mdproxy.mdpublicsuffix.mdquic-qlog.mdquic.mdtrace.mdwebdav.mdwebsocket.mdxsrftoken.md
tile.json

context-ctxhttp.mddocs/

HTTP with Context

Package ctxhttp provides helper functions for performing context-aware HTTP requests.

Import

import "golang.org/x/net/context/ctxhttp"

Functions

// Do sends an HTTP request with the provided http.Client and returns an HTTP response
func Do(ctx context.Context, client *http.Client, req *http.Request) (*http.Response, error)

// Get issues a GET request via the Do function
func Get(ctx context.Context, client *http.Client, url string) (*http.Response, error)

// Head issues a HEAD request via the Do function
func Head(ctx context.Context, client *http.Client, url string) (*http.Response, error)

// Post issues a POST request via the Do function
func Post(ctx context.Context, client *http.Client, url string, bodyType string, body io.Reader) (*http.Response, error)

// PostForm issues a POST request via the Do function
func PostForm(ctx context.Context, client *http.Client, url string, data url.Values) (*http.Response, error)

Function Details

Do

Do sends an HTTP request with the provided http.Client and returns an HTTP response.

If the client is nil, http.DefaultClient is used.

The provided ctx must be non-nil. If it is canceled or times out, ctx.Err() will be returned.

Get

Get issues a GET request via the Do function.

Head

Head issues a HEAD request via the Do function.

Post

Post issues a POST request via the Do function.

PostForm

PostForm issues a POST request via the Do function.

Usage Examples

Basic GET Request

import (
    "context"
    "fmt"
    "golang.org/x/net/context/ctxhttp"
    "net/http"
    "time"
)

func fetchWithTimeout(url string) error {
    // Create context with timeout
    ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
    defer cancel()

    // Make GET request with context
    resp, err := ctxhttp.Get(ctx, nil, url)
    if err != nil {
        return err
    }
    defer resp.Body.Close()

    fmt.Println("Status:", resp.Status)
    return nil
}

POST Request

func postData(url string, data io.Reader) error {
    ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
    defer cancel()

    resp, err := ctxhttp.Post(ctx, nil, url, "application/json", data)
    if err != nil {
        return err
    }
    defer resp.Body.Close()

    return nil
}

Custom HTTP Client

func fetchWithCustomClient(url string) error {
    client := &http.Client{
        Timeout: 30 * time.Second,
    }

    ctx := context.Background()
    resp, err := ctxhttp.Get(ctx, client, url)
    if err != nil {
        return err
    }
    defer resp.Body.Close()

    return nil
}