{"title":"Respond With","description":"Negotiate HTML, JSON, text, XML, and JavaScript responses in Amber V2, with an explicit Markdown route","section":"guides/controllers","version":"v2","path":"guides/controllers/respond-with","canonical_url":"https://amberframework.org/docs/v2/guides/controllers/respond-with","markdown_url":"https://amberframework.org/docs/v2/guides/controllers/respond-with.md","inherited":false,"content_markdown":"# Respond With\n\nUse `respond_with` when one controller action can return more than one content\ntype. Amber selects a response from the path extension or the request's\n`Accept` header.\n\n**File: `src/controllers/status_controller.cr` — add this action inside\n`StatusController`.**\n\n```crystal\nclass StatusController < ApplicationController\n  def show\n    respond_with do\n      html render(\"show.ecr\")\n      json({status: \"ok\"}.to_json)\n      text \"ok\"\n      xml \"<status>ok</status>\"\n    end\n  end\nend\n```\n\nSupported helpers and emitted media types are:\n\n| Helper | Media type |\n|---|---|\n| `html` | `text/html` |\n| `json` | `application/json; charset=utf-8` |\n| `text` | `text/plain` |\n| `xml` | `application/xml` |\n| `js` | `text/javascript` |\n\nEach helper accepts a string, a zero-argument block returning a string, or—for\nHTML—rendered ECR output. If Amber cannot match any available response, it\nreturns `406 Response Not Acceptable`.\n\nAmber `2.0.0-beta.5` does not ship a `markdown` responder helper. When the\napplication publishes Markdown, keep that representation explicit until a\ntagged framework release includes it.\n\n**File: `src/controllers/status_controller.cr` — add this second action inside\n`StatusController`.**\n\n```crystal\ndef show_markdown\n  response.content_type = \"text/markdown; charset=utf-8\"\n  \"# Status\\n\\nOK\\n\"\nend\n```\n\n**File: `config/routes.cr` — register the action inside the existing `routes\n:web` block.**\n\n```crystal\nget \"/status\", StatusController, :show\nget \"/status.md\", StatusController, :show_markdown\n```\n\n**File: `src/views/status/show.ecr` — create the HTML representation referenced\nby `render(\"show.ecr\")`.**\n\n```ecr\n<p>Status: ok</p>\n```\n\n**Run from: the application root while `amber watch` is running.**\n\n```bash\ncurl -H 'Accept: application/json' http://127.0.0.1:3000/status\ncurl http://127.0.0.1:3000/status.json\ncurl http://127.0.0.1:3000/status.md\n```\n\nKeep serialization explicit. For typed request parsing and structured API\nerrors, use the [Schema API](../schema-api/index.md)."}