{"id":13816927,"url":"https://github.com/EddieIvan01/nic","last_synced_at":"2025-05-15T18:32:41.391Z","repository":{"id":57484445,"uuid":"186646927","full_name":"EddieIvan01/nic","owner":"EddieIvan01","description":"🌀 Nic is a HTTP request client with elegant and easy-to-use API","archived":true,"fork":false,"pushed_at":"2019-11-28T15:47:05.000Z","size":1135,"stargazers_count":103,"open_issues_count":0,"forks_count":18,"subscribers_count":3,"default_branch":"master","last_synced_at":"2024-11-19T14:42:30.862Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mpl-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/EddieIvan01.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}},"created_at":"2019-05-14T15:08:15.000Z","updated_at":"2024-06-03T01:51:18.000Z","dependencies_parsed_at":"2022-08-26T14:24:08.322Z","dependency_job_id":null,"html_url":"https://github.com/EddieIvan01/nic","commit_stats":null,"previous_names":[],"tags_count":6,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/EddieIvan01%2Fnic","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/EddieIvan01%2Fnic/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/EddieIvan01%2Fnic/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/EddieIvan01%2Fnic/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/EddieIvan01","download_url":"https://codeload.github.com/EddieIvan01/nic/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":254397956,"owners_count":22064588,"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":[],"created_at":"2024-08-04T06:00:26.551Z","updated_at":"2025-05-15T18:32:41.099Z","avatar_url":"https://github.com/EddieIvan01.png","language":"Go","funding_links":[],"categories":["编程语言"],"sub_categories":["Golang"],"readme":"# Nic\n\n![GitHub release](https://img.shields.io/github/release/eddieivan01/nic.svg?label=nic)  ![GitHub issues](https://img.shields.io/github/issues/eddieivan01/nic.svg)\n\nEnglish | [中文](https://github.com/EddieIvan01/nic/tree/master/docs/zh-cn.md)\n\nNic is a HTTP request client which has elegant, easy-to-use API\n\n***\n\n## Features\n\n+ wrapper of HTTP std lib, provids elegant and easy-to-use API\n\n+ keep session via `nic.Session` structure, `nic.Session` is go-routine safe\n\n***\n\n## Installation\n\nTo install nic, enter the following command\n\n```\n$ go get -v -u github.com/eddieivan01/nic\n```\n\n***\n\n## Quick start\n\nDo a HTTP request like this\n\n```go\nresp, err := nic.Get(\"http://example.com\", nil)\nif err != nil {\n    log.Fatal(err.Error())\n}\nfmt.Println(resp.Text)\n```\n\n***\n\n## Documentation\n\n## do a basic request\n\nnic could do these methods' request\n\n`\"HEAD\", \"GET\", \"POST\", \"DELETE\", \"OPTIONS\", \"PUT\", \"PATCH\"`\n\n```go\nimport (\n\t\"fmt\"\n    \"github.com/eddieivan01/nic\"\n)\n\nfunc main() {\n    url := \"http://example.com\"\n    resp, err := nic.Get(url, nil)\n    if err != nil {\n        log.Fatal(err.Error())\n    }\n    fmt.Println(resp.Text)\n}\n```\n\n## post request with some form data\n\nas you see, all requests' parameters are passed by `nic.H`, and the inner is saved in `nic.KV`, it's actually `map[string]interface{}`\n\n```go\nresp, err := nic.Post(url, nic.H{\n    Data : nic.KV{\n        \"nic\" : \"nic\",\n    },\n    Headers : nic.KV{\n        \"X-Forwarded-For\" : \"127.0.0.1\",\n    },\n})\n```\n\n## request with cookies\n\nof course, you can also set it in Headers\n\n```go\nresp, err := nic.Get(url, nic.H{\n    Cookies : nic.KV{\n        \"cookie\" : \"nic\",\n    },\n})\n```\n\n## request with files\n\nyou can upload files with files' name + files' content which is `[]byte` type, and can also upload via local file path\n\nwhile uploading a file, you can set `multipart` form's field name, filename and MIME type\n\nfor more convenient  setting files parameters, you can invoke in a chain to set `filename` and MIME type\n\n```go\nresp, err := nic.Post(url, nic.H{\n    Files : nic.KV{\n        \"file1\": nic.File(\n                    \"nic.go\", \n                    []byte(\"package nic\")),\n        \"file2\": nic.FileFromPath(\"./nic.go\").\n                    MIME(\"text/plain\").\n                    FName(\"nic\"),\n    },\n})\n```\n\n## request with JSON\n\n```go\nresp, err := nic.Post(url, nic.H{\n    JSON : nic.KV{\n        \"nic\" : \"nic\",\n    }\n})\n```\n\n## request with unencoded raw message\n\n```go\nresp, err := nic.Post(url, nic.H{\n    Raw : \"post body which is unencoded\",\n})\n```\n\n## using chunked transfer\n\nThe default is not to use chunked transfer\n\nenable the `transfer-encoding: chunked`\n\n```go\nresp, _ := nic.Get(url, nic.H{\n    Chunked: true,\n})\n```\n\n## set query params\n\n```go\nresp, err := nic.Get(url, nic.H {\n    Params: nic.KV {\n        \"a\": \"1\",\n    },\n})\n```\n\n## all the parameters you could set\n\n```go\nH struct {\n    Params  KV\n    Data    KV\n    Raw     string\n    Headers KV\n    Cookies KV\n    Auth    KV\n    Proxy   string\n    JSON    KV\n    Files   KV\n\n    AllowRedirect      bool\n    Timeout            int64\n    Chunked            bool\n    DisableKeepAlives  bool\n    DisableCompression bool\n    SkipVerifyTLS      bool\n}\n```\n\n## NOTICE\n\n`nic.H` can only have one of the following four parameters\n\n`H.Raw, H.Data, H.Files, H.JSON`\n\n## request with session, which could handle server's `set-cookie` header\n\n```go\nsession := nic.NewSession()\nresp, err := session.Post(\"http://example.com/login\", nic.H{\n    Data : nic.KV{\n        \"uname\" : \"nic\",\n        \"passwd\" : \"nic\",\n    },\n})\n\n// ......\n\nresp, err = session.Get(\"http://example.com/userinfo\", nil)\n```\n\n## handle response\n\n```go\nresp, _ := nic.Get(url, nil)\nfmt.Println(resp.Text)\nfmt.Println(resp.Bytes)\n```\n\n## handle JSON response\n\n```go\nresp, _ := nic.Get(url, nil)\n\ntype S struct {\n    P1 string `json:\"p1\"`\n    P2 string `json:\"p2\"`\n}\n\ns := \u0026S{}\nerr := resp.JSON(\u0026s)\n\nif err == nil {\n    fmt.Println(s.P1, s.P2)\n}\n```\n\n## change response's encoding\n\n`SetEncode` will convert `resp.Bytes` to `resp.Text` if encoding is changed every time be called \n\n```go\nresp, _ := nic.Get(url, nil)\nerr := resp.SetEncode(\"gbk\")\n\nif err == nil {\n    fmt.Println(resp.Text)\n}\n```\n\n## save response's content as a file\n\n```go\nresp, _ := nic.Get(\"http://example.com/1.jpg\", nil)\nerr := resp.SaveFile(\"1.jpg\")\n```\n\n***\n\n## register a request/response hook\n\n```go\nsession := nic.NewSession()\nsession.RegisterBeforeReqHook(func(r *http.Request) error {\n    r.URL.RawQuery = \"nic=nic\"\n    return nil\n})\nsession.RegisterAfterRespHook(func(r *http.Response) error {\n    r.Header.Set(\"nic\", \"nic\")\n    return nil\n})\n\nsession.Get(url, nil)\n```\n\n***\n\n## QA\n\n+ Q:\n\n  How to get origin `*http.Request` from `nic.Session`?\n\n  A:\n\n  by `nic.Session.GetRequest` method\n\n+ Q:\n\n  How to pass origin `*http.Response` to goquery-like DOM-parsing-libs from `nic.Response`?\n\n  A:\n\n  use `resp, _ := nic.Get(...); resp.Response` to access origin anonymous structure `*http.Response`; and `(*http.Response).Body's IO.Reader` has been saved, you can  use `*http.Response` as if it were the original structure\n\n+ Q:\n\n  Redirection is allowed 10 times by default, how could I increase the number?\n\n  A:\n\n  by access `nic.Session.Client` then change its CheckRedirect property\n\n+ Q:\n\n  How to use the chunked transfer-encoding?\n\n  A:\n\n  by nic.H{Chunked: true}","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FEddieIvan01%2Fnic","html_url":"https://awesome.ecosyste.ms/projects/github.com%2FEddieIvan01%2Fnic","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FEddieIvan01%2Fnic/lists"}