{"id":29710668,"url":"https://github.com/fastbill/go-request","last_synced_at":"2025-07-31T00:04:29.969Z","repository":{"id":35000626,"uuid":"171440652","full_name":"fastbill/go-request","owner":"fastbill","description":"Simple but opinionated HTTP request client for Go specialized for JSON handling","archived":false,"fork":false,"pushed_at":"2023-08-30T14:25:24.000Z","size":28,"stargazers_count":4,"open_issues_count":1,"forks_count":1,"subscribers_count":5,"default_branch":"master","last_synced_at":"2024-06-20T14:07:42.200Z","etag":null,"topics":["go","golang","http-client","http-requests","json"],"latest_commit_sha":null,"homepage":"","language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/fastbill.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2019-02-19T09:01:20.000Z","updated_at":"2022-07-12T08:00:06.000Z","dependencies_parsed_at":"2024-06-19T13:34:20.856Z","dependency_job_id":"887d0408-4894-4bda-b273-0ac0a06d62f6","html_url":"https://github.com/fastbill/go-request","commit_stats":null,"previous_names":[],"tags_count":9,"template":false,"template_full_name":null,"purl":"pkg:github/fastbill/go-request","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fastbill%2Fgo-request","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fastbill%2Fgo-request/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fastbill%2Fgo-request/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fastbill%2Fgo-request/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/fastbill","download_url":"https://codeload.github.com/fastbill/go-request/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fastbill%2Fgo-request/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":266753988,"owners_count":23979145,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-07-23T02:00:09.312Z","response_time":66,"last_error":null,"robots_txt_status":null,"robots_txt_updated_at":null,"robots_txt_url":"https://github.com/robots.txt","online":true,"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":["go","golang","http-client","http-requests","json"],"created_at":"2025-07-23T21:39:19.108Z","updated_at":"2025-07-23T21:39:19.712Z","avatar_url":"https://github.com/fastbill.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Request [![Build Status](https://travis-ci.com/fastbill/go-request.svg?branch=master)](https://travis-ci.com/fastbill/go-request) [![Go Report Card](https://goreportcard.com/badge/github.com/fastbill/go-request)](https://goreportcard.com/report/github.com/fastbill/go-request) [![GoDoc](https://godoc.org/github.com/fastbill/go-request?status.svg)](https://godoc.org/github.com/fastbill/go-request)\n\n\u003e An opinionated but extremely easy to use HTTP request client for Go to make JSON request and retrieve the results\n\n## Description\nWith this request package you just need to define the structs or maps/slices that correspond to the JSON request and response body. Together with the parameters like URL, method and headers you can directly execute a request with `Do`. If the request body is not of type `io.Reader` already, it will be encoded as JSON. Also the response will be decoded back into the struct or map/slice you provided for the result. Request and response body are optional which means they can be `nil`.\n\nIf the request could be made but the response status code was not `2xx` an error of the type `HTTPError` from the package [httperrors](https://github.com/fastbill/go-httperrors) will be returned. The same happens if you specified an `ExpectedResponseCode` and that one was not matched by the actual response.\n\n## Example\n```go\nimport (\n    \"net/http\"\n    \"github.com/fastbill/go-request/v2\"\n)\n\ntype Input struct {\n\tRequestValue string `json:\"requestValue\"`\n}\n\ntype Output struct {\n\tResponseValue string `json:\"responseValue\"`\n}\n\nparams := request.Params{\n    URL:    \"https://example.com\",\n    Method: http.MethodPost,\n    Headers: map[string]string{\"my-header\":\"value\", \"another-header\":\"value2\"},\n    Body:   Input{RequestValue: \"someValueIn\"},\n    Query: map[string]string{\"key\": \"value\"},\n    Timeout: 10 * time.Second,\n    ExpectedResponseCode: 201,\n}\n\nresult := \u0026Output{}\nerr := request.Do(params, result)\n```\nAll parameters besides the `URL` and the `Method` are optional and can be omitted.\n\n### Accessing the response headers\nIf you need access to the headers of the http response, you can initialize a header map and pass it as a third argument to `Do`.\nIt will then be populated with the response headers that the server returns.\n\n```go\nresponseHeaders := http.Header{}\nerr := request.Do(params, result, responseHeaders)\n```\n\n### Using a custom http client\nIf you want to supply a custom http client to use for the request, you can use `DoWithCustomClient`.\nThe client needs to be of type `*http.Client`.\n\n```go\nerr := request.DoWithCustomClient(params, result)\n```\n\n### Retrieving the response as a string\nIf you want to retrieve the response body as a string, e.g. for debugging or testing purposes, you can use `DoWithStringResponse`.\n\n```go\nresult, err := request.DoWithStringResponse(params)\n```\n\n## Convenience wrappers\n```go\nerr := request.Get(\"http://example.com\", result)\n\nerr := request.Post(\"http://example.com\", Input{RequestValue: \"someValueIn\"}, result)\n```\n\n## Defaults\n* All `2xx` response codes are treated as success, all other codes lead to an error being returned, if you want to check for a specific response code set `ExpectedResponseCode` in the parameters\n* If an HTTPError is returned it contains the response body as message if there was one\n* The request package takes care of closing the response body after sending the request\n* The http client does not follow redirects\n* The http client timeout is set to 30 seconds, use the `Timeout` parameter in case you want to define a different timeout for one of the requests\n* `Accept` and `Content-Type` request header are set to `application/json` and can be overwritten via the Headers parameter\n* The parameters `Headers` and `Query` accept a simple `map[string]string`. If you want to pass `http.Header` or `url.Values` instead, wrap them in the provided `request.ReformatMap` helper function.\n\n## Streaming\nThe package allows the request body (`Body` property of `Params`) to be of type `io.Reader`. That way you can pass on request bodies to other services without parsing them.\n\n## Why?\nTo understand why this package was created have a look at the code that would be the native equivalent of the code shown in the example above.\n```go\nimport (\n\t\"bytes\"\n\t\"encoding/json\"\n\t\"net/http\"\n\t\"time\"\n)\n\ntype Input struct {\n\tRequestValue string `json:\"requestValue\"`\n}\n\ntype Output struct {\n\tResponseValue string `json:\"responseValue\"`\n}\n\nbuf := \u0026bytes.Buffer{}\nerr := json.NewEncoder(buf).Encode(\u0026Input{RequestValue: \"someValueIn\"})\nif err != nil {\n    return err\n}\n\nreq, err := http.NewRequest(http.MethodPost, url, buf)\nif err != nil {\n    return err\n}\n\nreq.Header.Set(\"Accept\", \"application/json\")\nreq.Header.Set(\"Content-Type\", \"application/json\")\nreq.Header.Set(\"my-header\", \"value\")\nreq.Header.Set(\"another-header\", \"value2\")\n\nq := req.URL.Query()\nq.Add(\"key\", \"value\")\nreq.URL.RawQuery = q.Encode()\n\nclient := \u0026http.Client{\n    Timeout: 30 * time.Second,\n    CheckRedirect: func(req *http.Request, via []*http.Request) error {\n        return http.ErrUseLastResponse\n    },\n}\n\nres, err := client.Do(req)\nif err != nil {\n    return err\n}\ndefer func() {\n    err = res.Body.Close()\n    // handle err somehow\n}()\n\nresult := \u0026Output{}\nerr = json.NewDecoder(res.Body).Decode(result)\n```\nThis shows the request package saves a lot of boilerplate code. Instead of around 35 lines we just write the 9 lines shown in the example. That way the code is much easier to read and maintain.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffastbill%2Fgo-request","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ffastbill%2Fgo-request","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffastbill%2Fgo-request/lists"}