{"id":21449328,"url":"https://github.com/kralizek/httpextensions","last_synced_at":"2025-07-14T20:31:33.601Z","repository":{"id":55976781,"uuid":"119194282","full_name":"Kralizek/HttpExtensions","owner":"Kralizek","description":"Classes and extension methods to make easier to work with REST APIs via HttpClient","archived":false,"fork":false,"pushed_at":"2022-12-07T20:37:52.000Z","size":65,"stargazers_count":2,"open_issues_count":1,"forks_count":2,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-06-29T05:04:51.649Z","etag":null,"topics":["dotnet-standard","http-client","querystring","rest-client"],"latest_commit_sha":null,"homepage":null,"language":"C#","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/Kralizek.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}},"created_at":"2018-01-27T19:13:28.000Z","updated_at":"2023-11-20T05:49:41.000Z","dependencies_parsed_at":"2023-01-24T11:46:00.848Z","dependency_job_id":null,"html_url":"https://github.com/Kralizek/HttpExtensions","commit_stats":null,"previous_names":[],"tags_count":6,"template":false,"template_full_name":null,"purl":"pkg:github/Kralizek/HttpExtensions","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Kralizek%2FHttpExtensions","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Kralizek%2FHttpExtensions/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Kralizek%2FHttpExtensions/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Kralizek%2FHttpExtensions/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Kralizek","download_url":"https://codeload.github.com/Kralizek/HttpExtensions/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Kralizek%2FHttpExtensions/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":265344830,"owners_count":23750566,"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":["dotnet-standard","http-client","querystring","rest-client"],"created_at":"2024-11-23T03:19:46.835Z","updated_at":"2025-07-14T20:31:33.319Z","avatar_url":"https://github.com/Kralizek.png","language":"C#","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Kralizek's HTTP Extensions\n\n## Overview\n\nThis repository contains a set of extensions to ease working with HTTP requests and response in .NET.\n\n### Kralizek.Extensions.Http\n\nThis package offers basic HTTP utilities.\n\n#### Query string\n\nThe `HttpQueryStringBuilder` class helps creating a valid query string using the same methodology of the `StringBuilder`.\n\n```csharp\nvar builder = new HttpQueryStringBuilder();\n\nbuilder.Add(\"foo\", \"bar\");\nbuilder.Add(\"hello\", \"world\");\nbuilder.Add(\"asd\", \"lol\");\n\nstring query = builder.BuildQuery();\n\nConsole.WriteLine(query);  // asd=lol\u0026foo=bar\u0026hello=world\n```\n\nThe `BuildQuery` method offers the possibility of collating items with the same key with a separator.\n\n```csharp\nvar builder = new HttpQueryStringBuilder();\n\nbuilder.Add(\"fields\", \"firstName\");\nbuilder.Add(\"fields\", \"lastName\");\n\nvar query = builder.BuildQuery(collateKeysBy: \",\");\n\nConsole.WriteLine(query);  // fields=firstName,lastName\n```\n\n### Kralizek.Extensions.Http.Json\n\nThis package offers support for JSON payloads.\n\n#### JSON payloads\n\nThe `JsonContent` class inherits from `System.Net.Http.HttpContent`, therefore it can be used to attach JSON payloads to an HTTP request.\n\nThe static factory method `FromObject\u003cT\u003e` accepts any object and uses _Newtonsoft.Json_ to serialize it into a JSON object.\n\n```csharp\nvar person = new Person\n{\n    FirstName = \"John\",\n    LastName = \"Doe\"\n};\n\nusing var request = new HttpRequestMessage(HttpMethod.Post, \"http://localtest.me:8080\") \n{\n    Content = JsonContent.FromObject(person)\n};\n\nusing var response = await http.SendAsync(request);\n```\n\n#### HTTP REST Client\n\nThe `HttpRestClient` is an opinionated wrapper around the `HttpClient`.\n\nHere are some advantages of using the `HttpRestClient`:\n\n- it delegates the `HttpClient` instance management to `IHttpClientFactory`,\n- offers methods to send HTTP requests with and without payload expecting or not a payload in the response,\n- it embeds logging using the `Microsoft.Extensions.Logging` framework,\n- it automatically handles serialization and deserialization to and from JSON payloads,\n- it integrates nicely with the `Microsoft.Extensions.DependencyInjection` framework.\n\nHere is a sample of its usage.\n\n```csharp\nvar client = services.GetRequiredServices\u003cIHttpRestClient\u003e();\n\nvar person = await client.SendAsync\u003cPerson\u003e(HttpMethod.Get, \"/people/1\", query);\n```\n\nIn the snippet above, the call to the `SendAsync` method takes care of:\n\n- getting an instance of `HttpClient` from the local `IHttpClientFactory`,\n- assemble the HTTP request using the specified HTTP method, the path and the query from the previous snippet,\n- log the HTTP request,\n- send the HTTP request,\n- receive the HTTP response,\n- log the HTTP response,\n- validate the result of the request via the status code of the HTTP response,\n- deserialize the payload into an instance of the `Person` class,\n- dispose all the resources that need disposing (`HttpRequestMessage` and `HttpResponseMessage`)\n\n#### Setup\n\nIn the package there are different utilities that allow a simple yet powerful setup of the `HttpRestClient`.\n\n```csharp\nservices.AddHttpRestClient(\"RequestBin\", builder =\u003e builder\n    .ConfigureHttpClient(http =\u003e\n    {\n        http.BaseAddress = new Uri(\"https://your-pipe.x.pipedream.net\");\n        http.DefaultRequestHeaders.Add(\"X-Test\", \"This is a test\");\n    })\n    .ConfigureHttpRestClient(options =\u003e\n    {\n        options.ContentMediaType = JsonContent.ApplicationJsonMediaType;\n    })\n    .ConfigureSerialization(json =\u003e \n    {\n        json.ContractResolver = new DefaultContractResolver \n        {\n            NamingStrategy = new CamelCaseNamingStrategy()\n        };\n\n        json.Formatting = Formatting.Indented;\n    }));\n```\n\nSince `AddHttpRestClient` accepts a delegate of type `Action\u003cIHttpClientBuilder\u003e`, you can leverage extensions to the `HttpClientFactory` API like [Polly](https://docs.microsoft.com/en-gb/aspnet/core/fundamentals/http-requests?view=aspnetcore-3.1#use-polly-based-handlers).\n\n## Versioning\n\nThis library follows [Semantic Versioning 2.0.0](http://semver.org/spec/v2.0.0.html) for the public releases (published to the [nuget.org](https://www.nuget.org/)).\n\n\n## How to build\n\nThis project uses [Cake](https://cakebuild.net/) as a build engine. You will also need the [.NET Core SDK 3.1.401](https://dotnet.microsoft.com/).\n\nIf you would like to build this project locally, just execute the `build.cake` script.\n\nYou can do it by using the .NET tool created by CAKE authors and use it to execute the build script.\n\n```powershell\ndotnet tool restore\ndotnet cake\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkralizek%2Fhttpextensions","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fkralizek%2Fhttpextensions","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkralizek%2Fhttpextensions/lists"}