Response Overview

HttpBuildR.Response is a set of extension methods that build a HttpResponseMessage. The usual place for this is a test that needs an expected response.

dotnet add package HttpBuildR.Response
using HttpBuildR;
using Resp = HttpStatusCode;

With implicit usings the using HttpBuildR; line is not needed.

Start with a status code

Result takes an optional reason phrase, the request the response answers, and an http version:

HttpResponseMessage response = HttpStatusCode.Created.Result();
HTTP/2.0 Created Created

The reason phrase defaults to the name of the status code.

Add headers

WithHeader takes any header. The typed helpers in the headers guide cover the standard ones.

response = response.WithHeader("X-Request-Id", "abc-123");
HTTP/2.0 Created Created
X-Request-ID: abc-123

Add content

Content helpers replace any existing content:

response = response.WithJsonContent(new { Name = "Ben", Age = 36 });
HTTP/2.0 Created Created
X-Request-ID: abc-123
Content-Type: application/json; charset=utf-8

{"Name":"Ben","Age":36}

Text, xml, form url encoded and raw stream content are all available, see the content guide.

See also