{"id":18326508,"url":"https://github.com/ghosind/go-request","last_synced_at":"2025-04-09T16:25:42.875Z","repository":{"id":97732454,"uuid":"564247547","full_name":"ghosind/go-request","owner":"ghosind","description":"An easy-to-use HTTP request tool for Golang.","archived":false,"fork":false,"pushed_at":"2024-07-22T13:58:28.000Z","size":230,"stargazers_count":0,"open_issues_count":2,"forks_count":0,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-02-15T09:46:06.857Z","etag":null,"topics":["go-library","go-module","go-package","golang-library","http-client","request","request-library"],"latest_commit_sha":null,"homepage":"https://pkg.go.dev/github.com/ghosind/go-request","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/ghosind.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}},"created_at":"2022-11-10T10:06:49.000Z","updated_at":"2024-07-22T13:58:31.000Z","dependencies_parsed_at":"2023-12-06T16:39:09.381Z","dependency_job_id":null,"html_url":"https://github.com/ghosind/go-request","commit_stats":null,"previous_names":[],"tags_count":3,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ghosind%2Fgo-request","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ghosind%2Fgo-request/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ghosind%2Fgo-request/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ghosind%2Fgo-request/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/ghosind","download_url":"https://codeload.github.com/ghosind/go-request/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248066346,"owners_count":21042091,"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":["go-library","go-module","go-package","golang-library","http-client","request","request-library"],"created_at":"2024-11-05T19:07:06.116Z","updated_at":"2025-04-09T16:25:42.813Z","avatar_url":"https://github.com/ghosind.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"# HTTP Request tool for Go\n\n![test](https://github.com/ghosind/go-request/workflows/test/badge.svg)\n[![Go Report Card](https://goreportcard.com/badge/github.com/ghosind/go-request)](https://goreportcard.com/report/github.com/ghosind/go-request)\n[![codecov](https://codecov.io/gh/ghosind/go-request/branch/main/graph/badge.svg)](https://codecov.io/gh/ghosind/go-request)\n![Version Badge](https://img.shields.io/github/v/release/ghosind/go-request)\n![License Badge](https://img.shields.io/github/license/ghosind/go-request)\n[![Go Reference](https://pkg.go.dev/badge/github.com/ghosind/go-request.svg)](https://pkg.go.dev/github.com/ghosind/go-request)\n\nEnglish | [简体中文](./README_CN.md)\n\nAn easy-to-use HTTP request tool for Golang.\n\n- [Features](#features)\n- [Installation](#installation)\n- [Getting Started](#getting-started)\n  - [`POST` and other requests](#post-and-other-requests)\n  - [Timeouts](#timeouts)\n  - [Response body handling](#response-body-handling)\n- [Client Instance](#client-instance)\n  - [Client Instance Config](#client-instance-config)\n- [Request Config](#request-config)\n- [Roadmap](#roadmap)\n\n## Features\n\n- Timeouts or self-control context.\n- Serialize request body automatically.\n- Response body deserialization wrapper.\n- Decode the compressed response body automatically.\n- Chaining API.\n- Request and Response interceptors.\n\n## Installation\n\n\u003e This package requires Go 1.18 and later versions.\n\nYou can install this package by the following command.\n\n```sh\ngo get -u github.com/ghosind/go-request\n```\n\n## Getting Started\n\nThe is a minimal example of performing a `GET` request:\n\n```go\nresp, err := request.Request(\"https://example.com/products/1\")\nif err != nil {\n  // handle error\n}\n// handle response\n```\n\n### `POST` and other requests\n\nYou can perform a `POST` request with a request config that set the `Method` field's value to `POST`.\n\n```go\nresp, err := request.Request(\"https://example.com/products\", RequestConfig{\n  Method: \"POST\",\n  Body:   map[string]any{\n    \"title\": \"Apple\",\n  },\n})\n// handle error or response\n```\n\nIf the `ContentType` field in the request config is empty, the body data will serialize to a JSON string default, and it'll also set the `Content-Type` field value in the request headers to `application/json`.\n\nYou can also use `POST` method to perform a `POST` request with the specific body data.\n\n```go\nresp, err := request.POST(\"https://example.com/products\", RequestConfig{\n  Body: map[string]any{\n    \"title\": \"Apple\",\n  },\n})\n// handle error or response\n```\n\nWe also provided the following methods for performing HTTP requests:\n\n- `DELETE`\n- `GET`\n- `HEAD`\n- `OPTIONS`\n- `PATCH`\n- `POST`\n- `PUT`\n\n\u003e The above methods will overwrite the `Method` field in the request config.\n\n### Timeouts\n\nAll the requests will set timeout to 1-second default, you can set a custom timeout value in milliseconds to a request:\n\n```go\nresp, err := request.Request(\"https://example.com\", request.RequestConfig{\n  Timeout: 3000, // 3 seconds\n})\n// handle error or response\n```\n\nYou can also set `Timeout` to `request.RequestTimeoutNone` to disable the timeout mechanism.\n\n\u003e The timeout will be disabled if you set `Context` in the request config, you need to handle it manually.\n\n### Chaining API\n\nYou can also make a request by chaining API: \n\n```go\nresp, err := request.Req(\"http://example.com\").\n  POST().\n  SetBody(map[string]any{ \"title\": \"Apple\" }).\n  SetTimeout(3000).\n  Do()\n```\n\n### Response body handling\n\nWe provided `ToObject` and `ToString` methods to handle response body. For example, the `ToString` method will read all data in the response body, and return it that represented in a string value.\n\n```go\ncontent, resp, err := ToString(request.Request(\"https://example.com/products/1\"))\nif err != nil {\n  // handle error\n}\n// handle response\n```\n\nThe `ToObject` method will read the content type of response and deserialize the body to the specific type.\n\n```go\ntype Product struct {\n  ID    int    `json:\"id\"`\n  Title string `json:\"title\"`\n}\n\nproduct, resp, err := ToObject[Product](request.Request(\"https://example.com/products/1\"))\nif err != nil {\n  // handle error\n}\n// handle response\n```\n\n\u003e Both `ToObject` and `ToString` methods will close the `Body` of the response after reading all data.\n\n## Client Instance\n\nYou can create a new client instance with a custom config.\n\n```go\ncli := request.New(request.Config{\n  BaseURL: \"https://example.com/\",\n})\n\nresp, err := cli.GET(\"/products/1\")\n// handle error or response\n```\n\n### Client Instance Config\n\n| Field | Type | Description |\n|:-----:|:----:|-------------|\n| `BaseURL` | `string` | The base url for all requests that performing by this client instance. |\n| `Headers` | `map[string][]string` | Custom headers to be sent. |\n| `MaxRedirects` | `int` | The maximum number of redirects for this client, default 5. |\n| `Parameters` | `map[string][]string` | Custom query string parameters to be sent. |\n| `Timeout` | `int` | Timeout in milliseconds. |\n| `UserAgent` | `string` | Custom user agent value. |\n| `ValidateStatus` | `func(int) bool` | The function checks whether the status code of the response is valid or not. |\n\n## Request Config\n\nThere are the available config options for performing a request, and all fields are optional.\n\n| Field | Type | Description |\n|:-----:|:----:|-------------|\n| `Auth` | `*BasicAuthConfig` | HTTP Basic Auth config. |\n| `BaseURL` | `string` | The base url for this requests. |\n| `Body` | `any` | The request body. |\n| `ContentType` | `string` | The content type of this request. Available options are: `\"json\"`, and default `\"json\"`. |\n| `Context` | `context.Context` | Self-control context. |\n| `DisableDecompress` | `bool` | Indicates whether or not disable decompression of the response body automatically. |\n| `Headers` | `map[string][]string` | Custom headers to be sent. |\n| `MaxRedirects` | `int` | The maximum number of redirects for the request, default 5. |\n| `Method` | `string` | HTTP request method, default `GET`. |\n| `Parameters` | `map[string][]string` | Custom query string parameters to be sent. |\n| `Timeout` | `int` | Timeout in milliseconds. |\n| `UserAgent` | `string` | Custom user agent value. |\n| `ValidateStatus` | `func(int) bool` | The function checks whether the status code of the response is valid or not. |\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fghosind%2Fgo-request","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fghosind%2Fgo-request","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fghosind%2Fgo-request/lists"}