{"title":"Respond With","description":"Respond to multiple request types easily and conveniently","section":"guides/controllers","version":"v1.5","path":"guides/controllers/respond-with","canonical_url":"https://amberframework.org/docs/v1.5/guides/controllers/respond-with","markdown_url":"https://amberframework.org/docs/v1.5/guides/controllers/respond-with.md","inherited":true,"content_markdown":"# Respond With\n\nIf we need to render a template to html `render(\"template.slang\")` works nicely. A lot of times we want to respond with json, xml, text or something else. In those cases, we can use `respond_with`.\n\nAmber will use 2 methods to determine which response type to use:\n\n1. The `accepts` header\n2. A url extension\n\nThese are the currently supported response types:\n\n```crystal\nhtml: \"text/html\"\njson: \"application/json\"\ntxt:  \"text/plain\"\ntext: \"text/plain\"\nxml:  \"application/xml\"\njs:   \"application/javascript\"\n```\n\n## Usage\n\n```crystal\nclass PetController < ApplicationController\n  def index\n    pets = Pet.all\n    respond_with do\n      html render(\"index.slang\")\n      \n      # Each response type also accepts a block\n      # The JSON response would be triggered from the following\n      # GET /pets/index.json\n      # GET /pets/index with the `accepts: application/json` header\n      json do \n        pets.to_json\n      end\n      \n      # This is another valid block format\n      xml { render(\"index.xml.slang\", layout: false) }\n      text { \"Here are your pets #{pets.try(&.join(\", \")}\" }\n      txt \"Here are your pets #{pets.try(&.join(\", \")}\"\n    end\n  end\nend\n```\n\nIf you do not specify a response type with the `accepts` header or using the dot notation in the url, you will get the first defined response type. In this example you would get the `html` response."}