---
url: https://jasperfx.github.io/alba/scenarios/text.md
description: >-
  Send plain text request bodies and read or assert on plain text responses in
  Alba scenarios.
---

# Working with Plain Text Requests

If you find yourself needing to test HTTP endpoints that either send text or return text, Alba
has you covered with some built in helpers.

## Reading the Response Text

To read the response body as text, use this syntax:

```cs
public async Task read_text(IAlbaHost host)
{
    var result = await host.Scenario(_ =>
    {
        _.Get.Url("/output");
    });

    // This deserializes the response body to the
    // designated Output type
    var outputString = await result.ReadAsTextAsync();

    // do assertions against the Output string
}
```

snippet source | anchor

## Assertions against the Response Text

You have these built in operations for asserting on the response body text:

```cs
public async Task assert_on_content(IAlbaHost host)
{
    await host.Scenario(_ =>
    {
        _.ContentShouldBe("exactly this");

        _.ContentShouldContain("some snippet");

        _.ContentShouldNotContain("some warning");
    });
}
```

snippet source | anchor

## Sending Text

Lastly, you can send text to an HTTP endpoint with this syntax:

```cs
public async Task send_text(IAlbaHost host)
{
    await host.Scenario(_ =>
    {
        _.Post.Text("some text").ToUrl("/textdata");
    });
}
```

snippet source | anchor

Do note that this also sets the `content-length` header to the string length and
sets the `content-type` header of the request to "text/plain."

## Sending Binary Data

Raw bytes or a stream can be written directly to the request body:

```cs
await host.Scenario(_ =>
{
    _.Post.ByteArray(bytes).ToUrl("/upload").ContentType("application/octet-stream");

    // or from any readable Stream
    _.Post.Stream(stream).ToUrl("/upload").ContentType("application/octet-stream");
});
```

Both set the `content-length` header from the written content; set the `content-type` header
yourself as shown.
