{"id":18303913,"url":"https://github.com/theopenlane/httpsling","last_synced_at":"2025-04-05T15:31:04.639Z","repository":{"id":255068663,"uuid":"848441834","full_name":"theopenlane/httpsling","owner":"theopenlane","description":"easy-to-use interface for sending http requests and handling responses","archived":false,"fork":false,"pushed_at":"2025-03-28T05:54:10.000Z","size":168,"stargazers_count":2,"open_issues_count":4,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-03-28T06:28:26.331Z","etag":null,"topics":["go","go-http","golang","golang-library","golang-package","http-requests"],"latest_commit_sha":null,"homepage":"","language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/theopenlane.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":".github/CONTRIBUTING.md","funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":".github/CODEOWNERS","security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2024-08-27T19:07:23.000Z","updated_at":"2025-01-26T00:32:25.000Z","dependencies_parsed_at":"2024-09-14T13:14:57.889Z","dependency_job_id":"24302dde-2d54-42d1-92d1-9f0a63980685","html_url":"https://github.com/theopenlane/httpsling","commit_stats":null,"previous_names":["theopenlane/httpsling"],"tags_count":4,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/theopenlane%2Fhttpsling","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/theopenlane%2Fhttpsling/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/theopenlane%2Fhttpsling/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/theopenlane%2Fhttpsling/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/theopenlane","download_url":"https://codeload.github.com/theopenlane/httpsling/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247358520,"owners_count":20926234,"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","go-http","golang","golang-library","golang-package","http-requests"],"created_at":"2024-11-05T15:27:17.852Z","updated_at":"2025-04-05T15:31:04.131Z","avatar_url":"https://github.com/theopenlane.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"[![Build status](https://badge.buildkite.com/f74a461120ffcadbf7796d5aac8ae8c03a1cbcfda142220074.svg)](https://buildkite.com/theopenlane/httpsling)\n[![Quality Gate Status](https://sonarcloud.io/api/project_badges/measure?project=theopenlane_httpsling\u0026metric=alert_status)](https://sonarcloud.io/summary/new_code?id=theopenlane_httpsling)\n[![Go Report Card](https://goreportcard.com/badge/github.com/theopenlane/httpsling)](https://goreportcard.com/report/github.com/theopenlane/httpsling)\n[![Go Reference](https://pkg.go.dev/badge/github.com/theopenlane/httpsling.svg)](https://pkg.go.dev/github.com/theopenlane/httpsling)\n[![License: Apache 2.0](https://img.shields.io/badge/License-Apache2.0-brightgreen.svg)](https://opensource.org/licenses/Apache-2.0)\n\n\n# Slinging HTTP\n\nThe `httpsling` library simplifies the way you make HTTP requests. It's intended to provide an easy-to-use interface for sending requests and handling responses, reducing the boilerplate code typically associated with the `net/http` package.\n\n## Overview\n\nCreating a new `Requester` and making a request should be straightforward:\n\n```go\npackage main\n\nimport (\n\t\"log\"\n\t\"net/http\"\n\n\t\"github.com/theopenlane/httpsling\"\n)\n\nfunc main() {\n\trequester, err := httpsling.New(\n\t\thttpsling.Client(), // use the default sling client\n\t\thttpsling.URL(\"https://api.example.com\"),\n\t)\n\tif err != nil {\n\t\tlog.Fatal(err)\n\t}\n\n\t// Perform a GET request\n\tvar out map[string]interface{}\n\tresp, err := requester.Receive(\u0026out, httpsling.Get(\"resource\"))\n\tif err != nil {\n\t\tlog.Fatal(err)\n\t}\n\n\tdefer resp.Body.Close()\n\n\tlog.Println(out)\n}\n```\n\n### Core Functions\n\n```go\n// build just the request\nRequest(...Option) (*http.Request, error)\nRequestWithContext(context.Context, ...Option) (*http.Request, error)\n\n// build the request and send the request\nSend(...Option) (*http.Response, error)\nSendWithContext(context.Context, ...Option) (*http.Response, error)\n\n// build and send the request and parse the response into an interface\nReceive(interface{}, ...Option) (*http.Response, []byte, error)\nReceiveWithContext(context.Context, interface{}, ...Option) (*http.Response, error)\n```\n\n### Configuring BaseURL\n\nSet the base URL for all requests using the `Requester` option `URL`:\n\n```go\n httpsling.URL(\"https://api.example.com\"),\n```\n\n### Setting Headers\n\nSet default headers for all requests, e.g. Bearer token Authorization\n\n```go\n    requester.Apply(httpsling.BearerAuth(\"YOUR_ACCESS_TOKEN\"))\n```\n\n### Setting up CookieJar\n\nAdd a Cookie Jar to the default client:\n\n```go\n    requester, err = httpsling.New(\n        httpsling.Client(\n            httpclient.CookieJar(nil), // Use a cookie jar to store cookies\n        ),\n    )\n    if err != nil {\n        return nil, err\n    }\n```\n\n### Configuring Timeouts\n\nDefine a global timeout for all requests to prevent indefinitely hanging operations:\n\n```go\n    httpsling.Client(\n        httpclient.Timeout(time.Duration(30*time.Second)),\n    ),\n```\n\n### TLS Configuration\n\nCustom TLS configurations can be applied for enhanced security measures, such as loading custom certificates:\n\n```go\n    httpsling.Client(\n        httpclient.SkipVerify(true),\n    ),\n```\n\n## Requests\n\nThe library provides a `Receive` to construct and dispatch HTTP. Here are examples of performing various types of requests, including adding query parameters, setting headers, and attaching a body to your requests.\n\n#### GET Request\n\n```go\n    resp, err := requester.ReceiveWithContext(context.Background(), \u0026out,\n        httpsling.Get(\"/path\"),\n        httpsling.QueryParam(\"query\", \"meow\"),\n    )\n```\n\n#### POST Request\n\n```go\n    resp, err := requester.ReceiveWithContext(context.Background(), \u0026out,\n        httpsling.Post(\"/path\"),\n        httpsling.Body(map[string]interface{}{\"key\": \"value\"})\n    )\n```\n\n#### PUT Request\n\n```go\n    resp, err := requester.ReceiveWithContext(context.Background(), \u0026out,\n        httpsling.Put(\"/path/123456\"),\n        httpsling.Body(map[string]interface{}{\"key\": \"newValue\"})\n    )\n```\n\n#### DELETE Request\n\n```go\n    resp, err := requester.ReceiveWithContext(context.Background(), \u0026out,\n        httpsling.Delete(\"/path/123456\"),\n    )\n```\n\n### Authentication\n\nSupports various authentication methods:\n\n- **Basic Auth**:\n\n```go\n    requester.Apply(httpsling.BasicAuth(\"username\", \"superSecurePassword!\"))\n```\n\n- **Bearer Token**:\n\n```go\n    requester.Apply(httpsling.BearerAuth(\"YOUR_ACCESS_TOKEN\"))\n```\n\n## Responses\n\nHandling responses is necessary in determining the outcome of your HTTP requests - the library has some built-in response code validators and other tasty things.\n\n```go\ntype APIResponse struct {\n    Data string `json:\"data\"`\n}\n\nvar out APIResponse\nresp, err := s.Requester.ReceiveWithContext(ctx, \u0026out,\n\t\thttpsling.Post(\"/path\"),\n\t\thttpsling.Body(in))\n\ndefer resp.Body.Close()\n\nlog.Printf(\"Status Code: %d\\n\", resp.StatusCode)\nlog.Printf(\"Response Data: %s\\n\", out.Data)\n```\n\n### Evaluating Response Success\n\nTo assess whether the HTTP request was successful:\n\n- **IsSuccess**: Check if the status code signifies a successful response\n\n```go\n    if httpsling.IsSuccess(resp) {\n        fmt.Println(\"The request succeeded hot diggity dog\")\n    }\n```\n\n## Inspirations\n\nThis library was inspired by and built upon the work of several other HTTP client libraries:\n\n- [Dghubble/sling](https://github.com/dghubble/sling)\n- [Monaco-io/request](https://github.com/monaco-io/request)\n- [Go-resty/resty](https://github.com/go-resty/resty)\n- [Fiber Client](https://github.com/gofiber/fiber)\n\n## Contributing\n\nSee [contributing](.github/CONTRIBUTING.md) for details.","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftheopenlane%2Fhttpsling","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ftheopenlane%2Fhttpsling","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftheopenlane%2Fhttpsling/lists"}