{"id":43604047,"url":"https://github.com/getmiranda/go-httpclient","last_synced_at":"2026-02-04T06:12:36.804Z","repository":{"id":40776026,"uuid":"456008352","full_name":"getmiranda/go-httpclient","owner":"getmiranda","description":null,"archived":false,"fork":false,"pushed_at":"2022-06-24T00:14:14.000Z","size":26,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"main","last_synced_at":"2024-07-02T20:18:02.320Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/getmiranda.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2022-02-05T23:13:49.000Z","updated_at":"2022-02-05T23:14:49.000Z","dependencies_parsed_at":"2022-09-22T08:31:47.262Z","dependency_job_id":null,"html_url":"https://github.com/getmiranda/go-httpclient","commit_stats":null,"previous_names":[],"tags_count":6,"template":false,"template_full_name":null,"purl":"pkg:github/getmiranda/go-httpclient","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/getmiranda%2Fgo-httpclient","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/getmiranda%2Fgo-httpclient/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/getmiranda%2Fgo-httpclient/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/getmiranda%2Fgo-httpclient/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/getmiranda","download_url":"https://codeload.github.com/getmiranda/go-httpclient/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/getmiranda%2Fgo-httpclient/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":29072814,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-02-04T03:31:03.593Z","status":"ssl_error","status_checked_at":"2026-02-04T03:29:50.742Z","response_time":62,"last_error":"SSL_connect returned=1 errno=0 peeraddr=140.82.121.5:443 state=error: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2026-02-04T06:12:34.605Z","updated_at":"2026-02-04T06:12:36.795Z","avatar_url":"https://github.com/getmiranda.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Go HTTP Client\n\nA production-ready HTTP client in Go with lots of useful features and using nothing more than the standard library of the language.\n\n## Installation\n\n```bash\n# Go Modules\nrequire github.com/getmiranda/go-httpclient\n```\n\nor\n\n```bash\ngo get github.com/getmiranda/go-httpclient\n````\n\n## Usage\n\nIn order to use the library for making HTTP calls you need to import the corresponding HTTP package:\n\n```go\nimport \"github.com/getmiranda/go-httpclient/gohttp\"\n```\n\n## Configuring the client\n\nOnce you have imported the package, you can now start using the client. First you need to configure and build the client as you need:\n\n```go\nheaders := make(http.Header)\nheaders.Set(\"Some-Common-Header\", \"value-for-all-requests\")\n\n// Create a new builder:\nhttpClient := gohttp.NewBuilder().\n\n    // You can set global headers to be used in every request made by this client:\n    SetHeaders(headers).\n\n    // Configure the base url to be used in every request made by this client:\n    SetBaseUrl(\"https://api.example.com\").\n\n    // Configure the timeout for getting a new connection:\n    SetConnectionTimeout(2 * time.Second).\n\n    // Configure the timeout for performing the actual HTTP call:\n    SetResponseTimeout(3 * time.Second).\n\n    // You can disable timeouts: \n    DisableTimeouts(true).\n\n    // You can pass a custom HTTP client:\n    SetHttpClient(http.DefaultClient).\n\n    // Configure the User-Agent header that will be used for all of the requests:\n    SetUserAgent(\"Your-User-Agent\").\n\n    // Configure the maximum idle (keep-alive) connections to keep per-host.\n    SetMaxIdleConnections(5).\n\n    // Configure the rate limit for the client.\n    SetRateLimiter(rate.Every(10*time.Second), 50). // 50 request every 10 seconds\n\n    // DisableKeepAlives disables keep-alives.\n    DisableKeepAlives(true).\n\n    // Finally, build the client and start using it!\n    Build()\n```\n\n## Performing HTTP calls\n\nThe `Client` interface provides convenient methods that you can use to perform different HTTP calls. If you get an error then you can safely ignore the response object since it won't be there.\n\n\u003e **Important:** There is no need to read \u0026 close anything from the response since the client is doing all of this for you. You just need to get the response and start using it!\n\nTake a look at all of the [EXAMPLES](examples) for more information.\n\n### Get\n\n```go\ntype Endpoints struct {\n    CurrentUserUrl    string `json:\"current_user_url\"`\n    AuthorizationsUrl string `json:\"authorizations_url\"`\n    RepositoryUrl     string `json:\"repository_url\"`\n}\n\nfunc GetEndpoints() (*Endpoints, error) {\n    // Make the request and wait for the response:\n    response, err := httpClient.Get(\"https://api.github.com\")\n    if err != nil {\n        // Deal with the error as you need:\n        return nil, err\n    }\n\n    // Interacting with the response:\n    fmt.Println(fmt.Sprintf(\"Status Code: %d\", response.StatusCode))\n    fmt.Println(fmt.Sprintf(\"Status: %s\", response.Status))\n    fmt.Println(fmt.Sprintf(\"Body: %s\\n\", response.String()))\n\n    // Processing JSON responses:\n    var endpoints Endpoints\n    if err := response.UnmarshalJson(\u0026endpoints); err != nil {\n        // Deal with the unmarshal error as you need:\n        return nil, err\n    }\n\n    fmt.Println(fmt.Sprintf(\"Repository URL: %s\", endpoints.RepositoryUrl))\n    return \u0026endpoints, nil\n}\n```\n\n### Post\n\n```go\n\n// The struct representing the actual JSON response from the API we're calling:\ntype GithubError struct {\n    StatusCode       int    `json:\"-\"`\n    Message          string `json:\"message\"`\n    DocumentationUrl string `json:\"documentation_url\"`\n}\n\n// The struct representing the JSON body we're going to send:\ntype Repository struct {\n    Name        string `json:\"name\"`\n    Description string `json:\"description,omitempty\"`\n    Private     bool   `json:\"private\"`\n}\n\nfunc CreateRepo(request Repository) (*Repository, error) {\n    // Make the request and wait for the response:\n    response, err := httpClient.Post(\"https://api.github.com/user/repos\", request)\n    if err != nil {\n        return nil, err\n    }\n\n    // Deal with failed status codes:\n    if response.StatusCode != http.StatusCreated {\n        var githubError GithubError\n        if err := response.UnmarshalJson(\u0026githubError); err != nil {\n            return nil, errors.New(\"error processing github error response when creating a new repo\")\n        }\n        return nil, errors.New(githubError.Message)\n    }\n\n    // Deal with successful response:\n    var result Repository\n    if err := response.UnmarshalJson(\u0026result); err != nil {\n        return nil, err\n    }\n    return \u0026result, nil\n}\n\n```\n\n## Testing\n\nThe library provides a convenient package for mocking requests and getting a particular response. The mock key is generated using the `HTTP method`, the `request URL` and the `request body`. Every request with these same elements will return the same mock.\n\nIn order to use the mocking features you need to import the corresponding package:\n\n```go\nimport \"github.com/getmiranda/go-httpclient/gohttp_testing\"\n```\n\n### Starting the mock server\n\n```go\nfunc TestMain(m *testing.M) {\n    // Tell the HTTP library to mock any further requests from here.\n    gohttp_testing.MockupServer.Start()\n\n    // Start the test cases for this pacakge:\n    os.Exit(m.Run())\n}\n```\n\nOnce you start the mock server, every request will be handled by this server and will not be sent against the real API. If there is no mock matching the current request you'll get an error saying ``no mock matching {METHOD} from '{URL}' with given body``.\n\n### Configuring a given HTTP mock\n\n```go\n// Delete all mocks in every new test case to ensure a clean environment:\ngohttp_testing.MockupServer.DeleteMocks()\n\n// Configure a new mock:\ngohttp_testing.MockupServer.AddMock(\u0026gohttp_testing.Mock{\n    Method:      http.MethodPost,\n    Url:         \"https://api.github.com/user/repos\",\n    RequestBody: `{\"name\":\"test-repo\",\"private\":true}`,\n\n    Error: errors.New(\"timeout from github\"),\n})\n```\n\nIn this case, we're telling the client that when we send a POST request against that URL and with that body, we want that particular error. In this case, no response was returned. Let's see how you can configure a particular response:\n\n```go\n// Delete all mocks in every new test case to ensure a clean environment:\ngohttp_testing.MockupServer.DeleteMocks()\n\n// Configure a new mock:\ngohttp_testing.MockupServer.AddMock(\u0026gohttp_testing.Mock{\n    Method:      http.MethodPost,\n    URL:         \"https://api.github.com/user/repos\",\n    RequestBody: `{\"name\":\"test-repo\",\"private\":true}`,\n\n    ResponseStatusCode: http.StatusCreated,\n    ResponseHeaders:    make(http.Header)\n    ResponseBody:       `{\"id\":123,\"name\":\"test-repo\"}`,\n})\n```\n\nIn this case, we get a response with status code `201 Created` and that particular response body.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgetmiranda%2Fgo-httpclient","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fgetmiranda%2Fgo-httpclient","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgetmiranda%2Fgo-httpclient/lists"}