{"metadata":{"image":[],"title":"","description":""},"api":{"url":"","auth":"required","results":{"codes":[]},"settings":"","params":[]},"next":{"description":"","pages":[]},"title":"Response","type":"basic","slug":"response","excerpt":"","body":"Sometimes a custom `Response` object needs to be created instead of using something that is `ResponseRepresentable`. For instance, if you need to manually assign cookies.\n[block:api-header]\n{\n  \"type\": \"basic\",\n  \"title\": \"Initialization\"\n}\n[/block]\nThere are various convenience intializers for Response.\n[block:api-header]\n{\n  \"type\": \"basic\",\n  \"title\": \"Empty\"\n}\n[/block]\nEmpty responses are useful when the Status is the important content.\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"Response(status: .created)\",\n      \"language\": \"swift\"\n    }\n  ]\n}\n[/block]\n\n[block:api-header]\n{\n  \"type\": \"basic\",\n  \"title\": \"Text\"\n}\n[/block]\nText responses are created when you return a `String` in a route closure, but they can also be created manually.\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"Response(status: .enhanceYourCalm, text: \\\"Too many requests\\\")\",\n      \"language\": \"swift\"\n    }\n  ]\n}\n[/block]\n\n[block:api-header]\n{\n  \"type\": \"basic\",\n  \"title\": \"JSON\"\n}\n[/block]\nJSON responses are created when you return `Json()` in a route closure, but they can also be created manually.\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"Response(status: .internalServerError, text: JSON([\\n    \\\"message\\\": \\\"Uh oh\\\"\\n]))\",\n      \"language\": \"swift\"\n    }\n  ]\n}\n[/block]\n\n[block:api-header]\n{\n  \"type\": \"basic\",\n  \"title\": \"Redirect\"\n}\n[/block]\nRedirect the client to a different URL with a 301 status code.\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"Response(redirect: \\\"http://google.com\\\")\",\n      \"language\": \"swift\"\n    }\n  ]\n}\n[/block]\n\n[block:api-header]\n{\n  \"type\": \"basic\",\n  \"title\": \"Cookies\"\n}\n[/block]\nTo add cookies to the response, make sure it is mutable and set them on the `cookies` property.\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"app.get(\\\"cookie\\\") { request in\\n    var response = Response(status: .ok, text: \\\"Cookie set\\\")\\n    response.cookies[\\\"id\\\"] = \\\"123\\\"\\n\\n    return response\\n}\\n\",\n      \"language\": \"swift\"\n    }\n  ]\n}\n[/block]\n\n[block:api-header]\n{\n  \"type\": \"basic\",\n  \"title\": \"Async\"\n}\n[/block]\nIf you need to wait for a callback to complete before sending your response, you will want to use the async response initializer. It will give you a `Stream` object to which you can send `Data` and close when you are finished.\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"Response(async: { stream in\\n    try stream.send(\\\"hi\\\".data, timingOut: 20)\\n\\t\\ttry stream.close()\\n})\",\n      \"language\": \"swift\"\n    }\n  ]\n}\n[/block]","updates":[],"order":4,"isReference":false,"hidden":false,"sync_unique":"","link_url":"","link_external":false,"_id":"5768701b72cd4c0e000fad21","user":"57086f3d447c410e00221bd1","category":{"sync":{"isSync":false,"url":""},"pages":[],"title":"Guide","slug":"getting-started","order":1,"from_sync":false,"reference":false,"_id":"5768701b72cd4c0e000fad1a","createdAt":"2016-04-09T04:31:52.489Z","__v":0,"version":"5768701b72cd4c0e000fad18","project":"57086fe855a4690e005de407"},"githubsync":"","version":{"version":"0.12","version_clean":"0.12.0","codename":"Client Clyde","is_stable":false,"is_beta":true,"is_hidden":false,"is_deprecated":true,"categories":["5768701b72cd4c0e000fad19","5768701b72cd4c0e000fad1a","5768701b72cd4c0e000fad1b","5768701b72cd4c0e000fad1c"],"_id":"5768701b72cd4c0e000fad18","releaseDate":"2016-06-20T22:37:15.641Z","__v":1,"createdAt":"2016-06-20T22:37:15.641Z","project":"57086fe855a4690e005de407"},"__v":0,"project":"57086fe855a4690e005de407","parentDoc":null,"createdAt":"2016-04-10T00:13:55.071Z"}
Sometimes a custom `Response` object needs to be created instead of using something that is `ResponseRepresentable`. For instance, if you need to manually assign cookies. [block:api-header] { "type": "basic", "title": "Initialization" } [/block] There are various convenience intializers for Response. [block:api-header] { "type": "basic", "title": "Empty" } [/block] Empty responses are useful when the Status is the important content. [block:code] { "codes": [ { "code": "Response(status: .created)", "language": "swift" } ] } [/block] [block:api-header] { "type": "basic", "title": "Text" } [/block] Text responses are created when you return a `String` in a route closure, but they can also be created manually. [block:code] { "codes": [ { "code": "Response(status: .enhanceYourCalm, text: \"Too many requests\")", "language": "swift" } ] } [/block] [block:api-header] { "type": "basic", "title": "JSON" } [/block] JSON responses are created when you return `Json()` in a route closure, but they can also be created manually. [block:code] { "codes": [ { "code": "Response(status: .internalServerError, text: JSON([\n \"message\": \"Uh oh\"\n]))", "language": "swift" } ] } [/block] [block:api-header] { "type": "basic", "title": "Redirect" } [/block] Redirect the client to a different URL with a 301 status code. [block:code] { "codes": [ { "code": "Response(redirect: \"http://google.com\")", "language": "swift" } ] } [/block] [block:api-header] { "type": "basic", "title": "Cookies" } [/block] To add cookies to the response, make sure it is mutable and set them on the `cookies` property. [block:code] { "codes": [ { "code": "app.get(\"cookie\") { request in\n var response = Response(status: .ok, text: \"Cookie set\")\n response.cookies[\"id\"] = \"123\"\n\n return response\n}\n", "language": "swift" } ] } [/block] [block:api-header] { "type": "basic", "title": "Async" } [/block] If you need to wait for a callback to complete before sending your response, you will want to use the async response initializer. It will give you a `Stream` object to which you can send `Data` and close when you are finished. [block:code] { "codes": [ { "code": "Response(async: { stream in\n try stream.send(\"hi\".data, timingOut: 20)\n\t\ttry stream.close()\n})", "language": "swift" } ] } [/block]