golang package to DRY making http client calls
Install
go get -v github.com/bikbah/httpcUse:
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> }