Skip to main content

Overview

PlainTextResponse is a response class that returns plain text content with the text/plain media type. It’s useful when you need to return raw text data without HTML markup or JSON serialization.

Import

Signature

Parameters

str | bytes
required
The plain text content to return
int
default:"200"
HTTP status code
dict[str, str] | None
Additional HTTP headers
str
default:"text/plain"
Media type for the response
BackgroundTask | None
Background task to run after sending the response

Properties

str
Always "text/plain" unless explicitly overridden

Usage

Basic plain text response

Using response_class parameter

When using response_class=PlainTextResponse, you can return a string directly without wrapping it in PlainTextResponse().

With custom status code

With custom headers

With background task

Use cases

Log files

Return log file contents as plain text

Configuration

Return configuration files or environment data

Text data

Return CSV, TSV, or other text-based formats

Debug output

Return debugging information as plain text

Example: Returning a text file

Example: CSV data

While you can set a custom media_type, consider using FileResponse for actual file downloads with proper headers.

Comparison with other response types

  • JSONResponse: Use for structured data that needs to be parsed
  • HTMLResponse: Use for HTML content to be rendered in a browser
  • PlainTextResponse: Use for raw text data, logs, or simple text formats