Gotcha is an high level HTTP client with a got-like API

Related tags

HTTP Clients gotcha
Overview

gotcha

Go Reference

Gotcha is an alternative to Go's http client, with an API inspired by got. It can interface with other HTTP packages through an adapter.

Example adapter implementations of fhttp & cclient can be found in the examples directory.

Usage

Top-Level API

Gotcha exposes a top-level API to make quick and simple requests:

... } ">
package main

import (
	"fmt"
	"github.com/sleeyax/gotcha"
	"log"
)

func main() {
	res, err := gotcha.Get("https://sleeyax.dev")
	if err != nil {
		log.Fatal(err)
	}
	fmt.Println(res.Text())
	// Output: ...
}

Configuration options

When you require further customization of the request, you can do so by specifying configuration Options:

package main

import (
	"fmt"
	"github.com/sleeyax/gotcha"
	"log"
)

func main() {
	res, _ := gotcha.Post("https://httpbin.org/anything", &gotcha.Options{
		Json: gotcha.JSON{
			"hello": "world",
		},
		FollowRedirect: false,
	})
	body, _ := res.Json()
	defer res.Close()
    fmt.Println(body["data"])
	// Output: {"hello": "world"}
}

Client

For advanced requests, create a client instance. Clients are configurable, extendable & reusable. This gives you fine-grained control over the request:

package main

import (
	"fmt"
	"github.com/sleeyax/gotcha"
	"io"
	"log"
	"net/http"
	"strings"
)

func main() {
	client, _ := gotcha.NewClient(&gotcha.Options{
		PrefixURL: "https://httpbin.org/",
		Headers: http.Header{
			"user-agent": {"gotcha by Sleeyax (https://github.com/sleeyax/gotcha)"},
		},
	})

	logClient, _ := client.Extend(&gotcha.Options{
		Hooks: gotcha.Hooks{
			Init: []gotcha.InitHook{
				func(o *gotcha.Options) {
					fmt.Println(fmt.Sprintf("about to send a request to %s with method %s", o.FullUrl.String(), o.Method))
				},
			},
		},
	})

	res, _ := logClient.DoRequest("PUT", "anything", &gotcha.Options{
		Body: io.NopCloser(strings.NewReader("hello world!")),
	})
	defer res.Close()
	// Output: "about to send a request to https://httpbin.org/anything with method PUT"
}
You might also like...
An enhanced HTTP client for Go
An enhanced HTTP client for Go

Heimdall Description Installation Usage Making a simple GET request Creating a hystrix-like circuit breaker Creating a hystrix-like circuit breaker wi

Enriches the standard go http client with retry functionality.

httpRetry Enriches the standard go http client with retry functionality using a wrapper around the Roundtripper interface. The advantage of this libra

http client for golang
http client for golang

Request HTTP client for golang, Inspired by Javascript-axios Python-request. If you have experience about axios or requests, you will love it. No 3rd

Simple HTTP and REST client library for Go

Resty Simple HTTP and REST client library for Go (inspired by Ruby rest-client) Features section describes in detail about Resty capabilities Resty Co

A nicer interface for golang stdlib HTTP client

rq A nicer interface for golang stdlib HTTP client Documents rq: here client: here jar: here Why? Because golang HTTP client is a pain in the a... Fea

a Go HTTP client with timeouts

go-httpclient requires Go 1.1+ as of v0.4.0 the API has been completely re-written for Go 1.1 (for a Go 1.0.x compatible release see 1adef50) Provides

GoRequest -- Simplified HTTP client ( inspired by nodejs SuperAgent )
GoRequest -- Simplified HTTP client ( inspired by nodejs SuperAgent )

GoRequest GoRequest -- Simplified HTTP client ( inspired by famous SuperAgent lib in Node.js ) "Shooting Requests like a Machine Gun" - Gopher Sending

gout to become the Swiss Army Knife of the http client @^^@--->  gout 是http client领域的瑞士军刀,小巧,强大,犀利。具体用法可看文档,如使用迷惑或者API用得不爽都可提issues
gout to become the Swiss Army Knife of the http client @^^@--- gout 是http client领域的瑞士军刀,小巧,强大,犀利。具体用法可看文档,如使用迷惑或者API用得不爽都可提issues

gout gout 是go写的http 客户端,为提高工作效率而开发 构架 feature 支持设置 GET/PUT/DELETE/PATH/HEAD/OPTIONS 支持设置请求 http header(可传 struct,map,array,slice 等类型) 支持设置 URL query(可

Retry, Race, All, Some, etc strategies for http.Client calls

reqstrategy Package reqstrategy provides functions for coordinating http.Client calls. It wraps typical call strategies like making simultaneous reque

Comments
  • Host removed after redirect

    Host removed after redirect

    Hey, I like your respository a lot! I was wondering if you could add an option to not delete the sensitive headers like "host" after a redirect to another host?

    Thank you very much in advance :)

    opened by fr-edy 2
Releases(v0.2.0)
  • v0.2.0(Sep 10, 2022)

    Most notable changes:

    • Fixed race conditions. Now you can just have one client instance doing a bunch of requests concurrently!
    // ...
    client, _ := NewClient(&Options{
    	PrefixURL: "https://example.com",
    })
    
    var wg sync.WaitGroup
    
    sendRequest := func() {
    	client.Get("test", &Options{
    		SearchParams: urlValues.Values{
    			"foo": {"bar"},
    			"abc": {"def"},
    		},
    	})
    
    	wg.Done()
    }
    
    for i := 0; i < 10; i++ {
    	wg.Add(1)
    	go sendRequest()
    }
    
    wg.Wait()
    
    // At this point 10 requests have been sent concurrently.
    
    • Fixed several small bugs
    • Updated dependencies
    • Breaking change: renamed client.Update to client.Put (this was a typo, but still).
    • Breaking change: removed the custom MaxRetriesExceededError struct and replaced it with a regular error message instead.

    Before:

    res, err := client.Get("https://example.com/")
    if _, ok := err.(*MaxRedirectsExceededError); !ok {
       // error is NOT MaxRedirectsExceededError
    }
    

    Now:

    res, err := client.Get("https://example.com/")
    if err != MaxRetriesExceededError {
       // error is NOT MaxRedirectsExceededError
    }
    

    Related commits: https://github.com/sleeyax/gotcha/compare/v0.1.0...v0.2.0

    Source code(tar.gz)
    Source code(zip)
Owner
Sleeyax
Using the right tools for the right job
Sleeyax
Http client call for golang http api calls

httpclient-call-go This library is used to make http calls to different API services Install Package go get

pzenteno 16 Oct 7, 2022
Speak HTTP like a local. (the simple, intuitive HTTP console, golang version)

http-gonsole This is the Go port of the http-console. Speak HTTP like a local Talking to an HTTP server with curl can be fun, but most of the time it'

mattn 65 Jul 14, 2021
fhttp is a fork of net/http that provides an array of features pertaining to the fingerprint of the golang http client.

fhttp The f stands for flex. fhttp is a fork of net/http that provides an array of features pertaining to the fingerprint of the golang http client. T

Flexagon 61 Jan 1, 2023
A Go HTTP client library for creating and sending API requests

Sling Sling is a Go HTTP client library for creating and sending API requests. Slings store HTTP Request properties to simplify sending requests and d

Dalton Hubble 1.5k Jan 7, 2023
NATS HTTP Round Tripper - This is a Golang http.RoundTripper that uses NATS as a transport.

This is a Golang http.RoundTripper that uses NATS as a transport. Included is a http.RoundTripper for clients, a server that uses normal HTTP Handlers and any existing http handler mux and a Caddy Server transport.

R.I.Pienaar 81 Dec 6, 2022
Simple HTTP package that wraps net/http

Simple HTTP package that wraps net/http

Kris 0 Jan 17, 2022
Http-conection - A simple example of how to establish a HTTP connection using Golang

A simple example of how to establish a HTTP connection using Golang

Jonathan Gonzaga 0 Feb 1, 2022
Full-featured, plugin-driven, extensible HTTP client toolkit for Go

gentleman Full-featured, plugin-driven, middleware-oriented toolkit to easily create rich, versatile and composable HTTP clients in Go. gentleman embr

Tom 986 Dec 23, 2022
An enhanced http client for Golang

go-http-client An enhanced http client for Golang Documentation on go.dev ?? This package provides you a http client package for your http requests. Y

Furkan Bozdag 51 Dec 23, 2022