{"id":14989943,"url":"https://github.com/ddliu/go-httpclient","last_synced_at":"2025-05-16T17:07:00.595Z","repository":{"id":14125688,"uuid":"16830869","full_name":"ddliu/go-httpclient","owner":"ddliu","description":"Advanced HTTP client for golang","archived":false,"fork":false,"pushed_at":"2023-07-03T07:07:02.000Z","size":119,"stargazers_count":464,"open_issues_count":6,"forks_count":106,"subscribers_count":29,"default_branch":"master","last_synced_at":"2025-04-12T15:58:49.644Z","etag":null,"topics":["curl","go","golang","http","httpclient"],"latest_commit_sha":null,"homepage":null,"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/ddliu.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","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":"2014-02-14T08:03:52.000Z","updated_at":"2025-04-03T05:10:19.000Z","dependencies_parsed_at":"2024-06-18T12:40:41.964Z","dependency_job_id":null,"html_url":"https://github.com/ddliu/go-httpclient","commit_stats":null,"previous_names":[],"tags_count":20,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ddliu%2Fgo-httpclient","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ddliu%2Fgo-httpclient/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ddliu%2Fgo-httpclient/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ddliu%2Fgo-httpclient/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/ddliu","download_url":"https://codeload.github.com/ddliu/go-httpclient/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":254573588,"owners_count":22093731,"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","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":["curl","go","golang","http","httpclient"],"created_at":"2024-09-24T14:19:12.113Z","updated_at":"2025-05-16T17:07:00.560Z","avatar_url":"https://github.com/ddliu.png","language":"Go","readme":"# go-httpclient \n\n[![Travis](https://img.shields.io/travis/ddliu/go-httpclient.svg?style=flat-square)](https://travis-ci.org/ddliu/go-httpclient)\n[![godoc](https://img.shields.io/badge/godoc-reference-blue.svg?style=flat-square)](https://godoc.org/github.com/ddliu/go-httpclient)\n[![License](https://img.shields.io/badge/license-MIT-blue.svg?style=flat-square)](LICENSE)\n[![Go Report Card](https://goreportcard.com/badge/github.com/ddliu/go-httpclient)](https://goreportcard.com/report/github.com/ddliu/go-httpclient)\n[![Coverage Status](https://coveralls.io/repos/github/ddliu/go-httpclient/badge.svg?branch=master)](https://coveralls.io/github/ddliu/go-httpclient?branch=master)\n\nAdvanced HTTP client for golang.\n\n## Features\n\n- Chainable API\n- Direct file upload\n- Timeout\n- HTTP Proxy\n- Cookie\n- GZIP\n- Redirect Policy\n- Cancel(with context)\n\n## Installation\n\n```bash\ngo get github.com/ddliu/go-httpclient\n```\n\n## Quick Start\n\n```go\npackage main\n\nimport (\n    \"github.com/ddliu/go-httpclient\"\n)\n\nfunc main() {\n    httpclient.Defaults(httpclient.Map {\n        httpclient.OPT_USERAGENT: \"my awsome httpclient\",\n        \"Accept-Language\": \"en-us\",\n    })\n\n    res, err := httpclient.Get(\"http://google.com/search\", map[string]string{\n        \"q\": \"news\",\n    })\n\n    println(res.StatusCode, err)\n}\n```\n\n## Usage\n\n### Setup\n\nUse `httpclient.Defaults` to setup default behaviors of the HTTP client.\n\n```go\nhttpclient.Defaults(httpclient.Map {\n    httpclient.OPT_USERAGENT: \"my awsome httpclient\",\n    \"Accept-Language\": \"en-us\",\n})\n```\n\nThe `OPT_XXX` options define basic behaviours of this client, other values are \ndefault request headers of this request. They are shared between different HTTP \nrequests.\n\n\n### Sending Request\n\n```go\n// get\nhttpclient.Get(\"http://httpbin.org/get\", map[string]string{\n    \"q\": \"news\",\n})\n\n// get with url.Values\nhttpclient.Get(\"http://httpbin.org/get\", url.Values{\n    \"q\": []string{\"news\", \"today\"}\n})\n\n// post\nhttpclient.Post(\"http://httpbin.org/post\", map[string]string {\n    \"name\": \"value\"\n})\n\n// post file(multipart)\nhttpclient.Post(\"http://httpbin.org/multipart\", map[string]string {\n    \"@file\": \"/tmp/hello.pdf\",\n})\n\n// put json\nhttpclient.PutJson(\"http://httpbin.org/put\", \n`{\n    \"name\": \"hello\",\n}`)\n\n// delete\nhttpclient.Delete(\"http://httpbin.org/delete\")\n\n// options\nhttpclient.Options(\"http://httpbin.org\")\n\n// head\nhttpclient.Head(\"http://httpbin.org/get\")\n```\n\n### Customize Request\n\nBefore you start a new HTTP request with `Get` or `Post` method, you can specify\ntemporary options, headers or cookies for current request.\n\n```go\nhttpclient.\n    WithHeader(\"User-Agent\", \"Super Robot\").\n    WithHeader(\"custom-header\", \"value\").\n    WithHeaders(map[string]string {\n        \"another-header\": \"another-value\",\n        \"and-another-header\": \"another-value\",\n    }).\n    WithOption(httpclient.OPT_TIMEOUT, 60).\n    WithCookie(\u0026http.Cookie{\n        Name: \"uid\",\n        Value: \"123\",\n    }).\n    Get(\"http://github.com\")\n```\n\n### Response\n\nThe `httpclient.Response` is a thin wrap of `http.Response`.\n\n```go\n// traditional\nres, err := httpclient.Get(\"http://google.com\")\nbodyBytes, err := ioutil.ReadAll(res.Body)\nres.Body.Close()\n\n// ToString\nres, err = httpclient.Get(\"http://google.com\")\nbodyString, err := res.ToString()\n\n// ReadAll\nres, err = httpclient.Get(\"http://google.com\")\nbodyBytes, err := res.ReadAll()\n```\n\n### Handle Cookies\n\n```go\nurl := \"http://github.com\"\nhttpclient.\n    WithCookie(\u0026http.Cookie{\n        Name: \"uid\",\n        Value: \"123\",\n    }).\n    Get(url)\n\nfor _, cookie := range httpclient.Cookies() {\n    fmt.Println(cookie.Name, cookie.Value)\n}\n\nfor k, v := range httpclient.CookieValues() {\n    fmt.Println(k, v)\n}\n\nfmt.Println(httpclient.CookieValue(\"uid\"))\n```\n\n### Concurrent Safe\n\nIf you want to start many requests concurrently, remember to call the `Begin` \nmethod when you begin:\n\n```go\ngo func() {\n    httpclient.\n        Begin().\n        WithHeader(\"Req-A\", \"a\").\n        Get(\"http://google.com\")\n}()\ngo func() {\n    httpclient.\n        Begin().\n        WithHeader(\"Req-B\", \"b\").\n        Get(\"http://google.com\")\n}()\n\n```\n\n### Error Checking\n\nYou can use `httpclient.IsTimeoutError` to check for timeout error:\n\n```go\nres, err := httpclient.Get(\"http://google.com\")\nif httpclient.IsTimeoutError(err) {\n    // do something\n}\n```\n\n### Full Example\n\nSee `examples/main.go`\n\n## Options\n\nAvailable options as below:\n\n- `OPT_FOLLOWLOCATION`: TRUE to follow any \"Location: \" header that the server sends as part of the HTTP header. Default to `true`.\n- `OPT_CONNECTTIMEOUT`: The number of seconds or interval (with time.Duration) to wait while trying to connect. Use 0 to wait indefinitely.\n- `OPT_CONNECTTIMEOUT_MS`: The number of milliseconds to wait while trying to connect. Use 0 to wait indefinitely.\n- `OPT_MAXREDIRS`: The maximum amount of HTTP redirections to follow. Use this option alongside `OPT_FOLLOWLOCATION`.\n- `OPT_PROXYTYPE`: Specify the proxy type. Valid options are `PROXY_HTTP`, `PROXY_SOCKS4`, `PROXY_SOCKS5`, `PROXY_SOCKS4A`. Only `PROXY_HTTP` is supported currently. \n- `OPT_TIMEOUT`: The maximum number of seconds or interval (with time.Duration) to allow httpclient functions to execute.\n- `OPT_TIMEOUT_MS`: The maximum number of milliseconds to allow httpclient functions to execute.\n- `OPT_COOKIEJAR`: Set to `true` to enable the default cookiejar, or you can set to a `http.CookieJar` instance to use a customized jar. Default to `true`.\n- `OPT_INTERFACE`: TODO\n- `OPT_PROXY`: Proxy host and port(127.0.0.1:1080).\n- `OPT_REFERER`: The `Referer` header of the request.\n- `OPT_USERAGENT`: The `User-Agent` header of the request. Default to \"go-httpclient v{{VERSION}}\".\n- `OPT_REDIRECT_POLICY`: Function to check redirect.\n- `OPT_PROXY_FUNC`: Function to specify proxy.\n- `OPT_UNSAFE_TLS`: Set to `true` to disable TLS certificate checking.\n- `OPT_DEBUG`: Print request info.\n- `OPT_CONTEXT`: Set `context.context` (can be used to cancel request).\n- `OPT_BEFORE_REQUEST_FUNC`: Function to call before request is sent, option should be type `func(*http.Client, *http.Request)`.\n\n## Seperate Clients\n\nBy using the `httpclient.Get`, `httpclient.Post` methods etc, you are using a \ndefault shared HTTP client.\n\nIf you need more than one client in a single programme. Just create and use them\nseperately.\n\n```go\nc1 := httpclient.NewHttpClient().Defaults(httpclient.Map {\n    httpclient.OPT_USERAGENT: \"browser1\",\n})\n\nc1.Get(\"http://google.com/\")\n\nc2 := httpclient.NewHttpClient().Defaults(httpclient.Map {\n    httpclient.OPT_USERAGENT: \"browser2\",\n})\n\nc2.Get(\"http://google.com/\")\n\n```\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fddliu%2Fgo-httpclient","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fddliu%2Fgo-httpclient","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fddliu%2Fgo-httpclient/lists"}