Skip to content

Folders and files

NameName
Last commit message
Last commit date

Latest commit

History

6 Commits

Repository files navigation

httpc

PkgGoDev

golang package to DRY making http client calls

Usage

  1. Install

    go get -v github.com/bikbah/httpc
    
  2. Use:

    package main
    import (
    "context""fmt""github.com/bikbah/httpc"
    )
    funcmain() {
    client:=httpc.Must("https://httpbin.org")
    req:=httpc.GET("/get").
    WithQuery(httpc.String("query", "query_value")).
    WithHeader(httpc.String("Authorization", "Bearer some_token"))
    varresstruct {
    Argsmap[string]any`json:"args"`Headersmap[string]string`json:"headers"`URLstring`json:"url"`JSONmap[string]any`json:"json"`
    }
    err:=client.Do(context.Background(), req, &res, nil)
    fmt.Printf("%+v %v\n", res, err)
    // Output: {// Args:map[query:query_value]// Headers:map[// Accept-Encoding:gzip// Authorization:Bearer some_token// Host:httpbin.org// User-Agent:Go-http-client/2.0// ]// URL:https://httpbin.org/get?query=query_value// JSON:map[]// }// <nil>reqBody:=struct {
    Keystring`json:"key"`
    }{
    Key: "value",
    }
    res=struct {
    Argsmap[string]any`json:"args"`Headersmap[string]string`json:"headers"`URLstring`json:"url"`JSONmap[string]any`json:"json"`
    }{}
    req=httpc.POST("/post").
    WithJSONBody(reqBody)
    err=client.Do(context.Background(), req, &res, nil)
    fmt.Printf("%+v %v\n", res, err)
    // Output: {// Args:map[]// Headers:map[// Accept-Encoding:gzip// Content-Length:16// Content-Type:application/json// Host:httpbin.org// User-Agent:Go-http-client/2.0// ]// URL:https://httpbin.org/post// JSON:map[key:value]// }// <nil>
    }

About

golang http client helper

Topics

Resources

Stars

1 star

Watchers

1 watching

Forks

Releases

Used by

Contributors

Languages