{"metadata":{"image":[],"title":"","description":""},"api":{"url":"","auth":"required","results":{"codes":[]},"settings":"","params":[]},"next":{"description":"","pages":[]},"title":"Request","type":"basic","slug":"request","excerpt":"","body":"Every route closure gets passed an instance of `Request`. This allows you to access information about the request made to your application such as the URI, input data, and session. \n[block:api-header]\n{\n  \"type\": \"basic\",\n  \"title\": \"Data\"\n}\n[/block]\nAny input sent during the request, such as query, form-encoded, or JSON-encoded data can be accessed from the `data` property.\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"request.data[\\\"name\\\"]\",\n      \"language\": \"swift\"\n    }\n  ]\n}\n[/block]\n\n[block:callout]\n{\n  \"type\": \"info\",\n  \"title\": \"Order of Importance\",\n  \"body\": \"Since query and body data are both accessed through the `data` property, it is possible that both will contain a value with the same key. In these cases, query data will trump body data.\"\n}\n[/block]\n\n[block:api-header]\n{\n  \"type\": \"basic\",\n  \"title\": \"Polymorphic\"\n}\n[/block]\nSubscripting request data always returns an object that conforms to `Polymorphic`.\n\n`Polymorphic` is a common protocol used throughout the framework. It allows for objects of unknown types to be more easily transformed into typed values. \n\nFor example, if you are expecting the key `age` to be sent as input, and for the `age` to be an `Int`, you could use the `.int` property.\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"guard let age = request.data[\\\"age\\\"].int else {\\n    throw Abort.badRequest\\n}\",\n      \"language\": \"swift\"\n    }\n  ]\n}\n[/block]\n\n[block:api-header]\n{\n  \"type\": \"basic\",\n  \"title\": \"Path Indexable\"\n}\n[/block]\nNested values can be easily accessed using comma syntax.\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"let firstName = request.data[\\\"name\\\", \\\"first\\\"].string\",\n      \"language\": \"swift\"\n    }\n  ]\n}\n[/block]\nYou can use a mixture of `String` and `Int` to form your path.\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"let firstUserEmail = request.data[\\\"users\\\", 0, \\\"email\\\"].string\",\n      \"language\": \"swift\"\n    }\n  ]\n}\n[/block]\n\n[block:api-header]\n{\n  \"type\": \"basic\",\n  \"title\": \"Specific Access\"\n}\n[/block]\nIf you need to access the query, form-encoded, or JSON-encoded data specifically, you can.\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"request.data.query // StructuredData\\nrequest.data.json // JSON?\\nrequest.data.formEncoded // StructuredData?\\nrequest.data.mulipart // [String: MultiPart]?\",\n      \"language\": \"swift\"\n    }\n  ]\n}\n[/block]\n\n[block:api-header]\n{\n  \"type\": \"basic\",\n  \"title\": \"S4\"\n}\n[/block]\nThe `Request` type is based on the request from Open Swift's [S4](https://github.com/open-swift/S4) standards. This means you can pass requests from Vapor directly into any other S4 compatible project.\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"public var uri: C7.URI\\npublic var version: S4.Version\\npublic var headers: S4.Headers\\npublic var body: S4.Body\\npublic var storage: [String : Swift.Any]\\n\\npublic init(\\n    method: S4.Method, \\n    uri: C7.URI, \\n    version: S4.Version, \\n    headers: S4.Headers, \\n    body: S4.Body\\n)\",\n      \"language\": \"swift\"\n    }\n  ]\n}\n[/block]\n\n[block:api-header]\n{\n  \"type\": \"basic\",\n  \"title\": \"Method\"\n}\n[/block]\nThe `method` property contains the HTTP method used by the request. \n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"enum Method {\\n    case delete\\n    case get\\n    case head\\n    case post\\n    case put\\n    case connect\\n    case options\\n    case trace\\n    case patch\\n    case other(method: String)\\n}\",\n      \"language\": \"swift\"\n    }\n  ]\n}\n[/block]\n\n[block:api-header]\n{\n  \"type\": \"basic\",\n  \"title\": \"URI\"\n}\n[/block]\nThe `uri` property contains information about the uniform resource identifier used to identify a specific route or resource in your application.\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"struct URI {\\n    var scheme: String?\\n    var host: String?\\n    var port: Int?\\n    var path: String?\\n    var query: [String: [String?]]\\n    var fragment: String?\\n}\",\n      \"language\": \"swift\"\n    }\n  ]\n}\n[/block]\n\n[block:api-header]\n{\n  \"type\": \"basic\",\n  \"title\": \"Headers\"\n}\n[/block]\nThe `headers` property contains a dictionary of all of the request headers by key. The key is case insensitive and can contain one or more values (as per the HTTP spec).\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"let contentType = request.headers[\\\"content-type\\\"]\",\n      \"language\": \"swift\"\n    }\n  ]\n}\n[/block]\n\n[block:api-header]\n{\n  \"type\": \"basic\",\n  \"title\": \"Body\"\n}\n[/block]\nTo access the raw data from the body of the request, use the `body` property.\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"let data = request.body\",\n      \"language\": \"swift\"\n    }\n  ]\n}\n[/block]","updates":[],"order":5,"isReference":false,"hidden":false,"sync_unique":"","link_url":"","link_external":false,"_id":"5768701b72cd4c0e000fad22","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"},"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":"","parentDoc":null,"user":"57086f3d447c410e00221bd1","__v":0,"createdAt":"2016-05-07T05:38:47.396Z","project":"57086fe855a4690e005de407"}
Every route closure gets passed an instance of `Request`. This allows you to access information about the request made to your application such as the URI, input data, and session. [block:api-header] { "type": "basic", "title": "Data" } [/block] Any input sent during the request, such as query, form-encoded, or JSON-encoded data can be accessed from the `data` property. [block:code] { "codes": [ { "code": "request.data[\"name\"]", "language": "swift" } ] } [/block] [block:callout] { "type": "info", "title": "Order of Importance", "body": "Since query and body data are both accessed through the `data` property, it is possible that both will contain a value with the same key. In these cases, query data will trump body data." } [/block] [block:api-header] { "type": "basic", "title": "Polymorphic" } [/block] Subscripting request data always returns an object that conforms to `Polymorphic`. `Polymorphic` is a common protocol used throughout the framework. It allows for objects of unknown types to be more easily transformed into typed values. For example, if you are expecting the key `age` to be sent as input, and for the `age` to be an `Int`, you could use the `.int` property. [block:code] { "codes": [ { "code": "guard let age = request.data[\"age\"].int else {\n throw Abort.badRequest\n}", "language": "swift" } ] } [/block] [block:api-header] { "type": "basic", "title": "Path Indexable" } [/block] Nested values can be easily accessed using comma syntax. [block:code] { "codes": [ { "code": "let firstName = request.data[\"name\", \"first\"].string", "language": "swift" } ] } [/block] You can use a mixture of `String` and `Int` to form your path. [block:code] { "codes": [ { "code": "let firstUserEmail = request.data[\"users\", 0, \"email\"].string", "language": "swift" } ] } [/block] [block:api-header] { "type": "basic", "title": "Specific Access" } [/block] If you need to access the query, form-encoded, or JSON-encoded data specifically, you can. [block:code] { "codes": [ { "code": "request.data.query // StructuredData\nrequest.data.json // JSON?\nrequest.data.formEncoded // StructuredData?\nrequest.data.mulipart // [String: MultiPart]?", "language": "swift" } ] } [/block] [block:api-header] { "type": "basic", "title": "S4" } [/block] The `Request` type is based on the request from Open Swift's [S4](https://github.com/open-swift/S4) standards. This means you can pass requests from Vapor directly into any other S4 compatible project. [block:code] { "codes": [ { "code": "public var uri: C7.URI\npublic var version: S4.Version\npublic var headers: S4.Headers\npublic var body: S4.Body\npublic var storage: [String : Swift.Any]\n\npublic init(\n method: S4.Method, \n uri: C7.URI, \n version: S4.Version, \n headers: S4.Headers, \n body: S4.Body\n)", "language": "swift" } ] } [/block] [block:api-header] { "type": "basic", "title": "Method" } [/block] The `method` property contains the HTTP method used by the request. [block:code] { "codes": [ { "code": "enum Method {\n case delete\n case get\n case head\n case post\n case put\n case connect\n case options\n case trace\n case patch\n case other(method: String)\n}", "language": "swift" } ] } [/block] [block:api-header] { "type": "basic", "title": "URI" } [/block] The `uri` property contains information about the uniform resource identifier used to identify a specific route or resource in your application. [block:code] { "codes": [ { "code": "struct URI {\n var scheme: String?\n var host: String?\n var port: Int?\n var path: String?\n var query: [String: [String?]]\n var fragment: String?\n}", "language": "swift" } ] } [/block] [block:api-header] { "type": "basic", "title": "Headers" } [/block] The `headers` property contains a dictionary of all of the request headers by key. The key is case insensitive and can contain one or more values (as per the HTTP spec). [block:code] { "codes": [ { "code": "let contentType = request.headers[\"content-type\"]", "language": "swift" } ] } [/block] [block:api-header] { "type": "basic", "title": "Body" } [/block] To access the raw data from the body of the request, use the `body` property. [block:code] { "codes": [ { "code": "let data = request.body", "language": "swift" } ] } [/block]