|
| 1 | +--- |
| 2 | +layout: default |
| 3 | +title: About $Request and $Response |
| 4 | +type: about |
| 5 | +--- |
| 6 | + |
| 7 | +# Polaris |
| 8 | + |
| 9 | +## about_Polaris |
| 10 | + |
| 11 | +# SHORT DESCRIPTION |
| 12 | + |
| 13 | +These are two special variables that will be available for use during the scriptblock of any Polaris Route. $Request is used to represent information that was requested from the client. $Response is used to hold information that you would like to send back to the client. |
| 14 | + |
| 15 | +# LONG DESCRIPTION |
| 16 | + |
| 17 | +\$Request is tied to the `[PolarisRequest]` class and \$Response is a `[PolarisResponse]` the information on what properties and methods each class contains are described below |
| 18 | + |
| 19 | +## [PolarisRequest]\$Request |
| 20 | + |
| 21 | +| Property | Description | |
| 22 | +| ---------------------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------- | |
| 23 | +| `[object]$Body` | By default this is empty but is an `[object]` so it can be used to store any format of data. Useful for middlware parsers such as a json body converter. | |
| 24 | +| `[string]$BodyString` | This is the full body of the request in string format | |
| 25 | +| `[NameValueCollection]$Query` | Gets the query string included in the request. | |
| 26 | +| `[string[]]$AcceptTypes` | Gets the MIME types accepted by the client. | |
| 27 | +| `[string]$Method` | Gets the HTTP method specified by the client. (i.e. POST, GET, PUT, DELETE, etc.) | |
| 28 | +| `[uri]$URL` | Gets the Uri object requested by the client. | |
| 29 | +| `[string]$UserAgent` | Gets the user agent presented by the client. | |
| 30 | +| `[string]$ClientIP` | Gets the IP address of the client making the request. | |
| 31 | +| `[HttpListenerRequest]$RawRequest` | Gets the full raw [HttpListenerRequest](https://docs.microsoft.com/en-us/dotnet/api/system.net.httplistenerrequest?view=netframework-4.8). | |
| 32 | +| `[PSCustomObject]$Parameters` | Parameters extracted from url patters. See about_routing for more details. | |
| 33 | +| `[IPrincipal]$User` | The user making the request. The information stored here will depend on the authentication method set. | |
| 34 | +| `[CookieCollection]$Cookies` | Gets the cookies sent with the request. | |
| 35 | +| `[NameValueCollection]$Headers` | Gets the collection of header name/value pairs sent in the request. | |
| 36 | + |
| 37 | +## [PolarisResponse]\$Response |
| 38 | + |
| 39 | +### Properties |
| 40 | + |
| 41 | +| Property | Description | |
| 42 | +| ------------------------------------ | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | |
| 43 | +| `[byte[]]$ByteResponse` | Gets and sets a byte response to be sent to the client. You may set this directly but you should not run any of the `Send` methods as those methods will overwrite this property. | |
| 44 | +| `[string]$ContentType` | Gets and sets the content type to be sent to the client. | |
| 45 | +| `[WebHeaderCollection]$Headers` | Gets and sets the headers to be sent to the client. | |
| 46 | +| `[int]$StatusCode` | Gets and sets the status code to be sent to the client. | |
| 47 | +| `[Stream]$StreamResponse` | Gets and sets a stream response to be sent to the client. You may set this directly but you should not run any of the `Send` methods as those methods will overwrite this property. | |
| 48 | +| `[HttpListenerResponse]$RawResponse` | Gets the HttpListenerResponse for direct access to what will be sent to the client. | |
| 49 | + |
| 50 | +### Methods |
| 51 | + |
| 52 | +The `[PolarisResponse]` class provides a series of helper methods that can be used to send information to the client a bit easier than accessing the properties directly. |
| 53 | + |
| 54 | +| Method | Description | |
| 55 | +| ------------------------------------------------------ | ----------------------------------------------------------------------------------------------------------------------------------------------------- | |
| 56 | +| `Send()` | Sends an empty response to the client | |
| 57 | +| `Send([string]$stringResponse)` | Sets the value of the \$ByteResponse property to the value of the string passed as the argument. \*Note: You will still have to set the content type. | |
| 58 | +| `SendBytes([byte[]]$byteArray)` | Sets the value of $ByteResponse to your $ByteArray | |
| 59 | +| `Json([string]$stringResponse` | Sets the value of $ByteResponse to the value of the string passed as the argument and sets the $ContentType to `application/json` | |
| 60 | +| `SetHeader([string]$headerName, [string]$headerValue)` | Sets the value of a header | |
| 61 | +| `SetStatusCode([int]$StatusCode)` | Sets the value of \$StatusCode | |
| 62 | +| `SetContentType([string]$ContentType)` | Sets the MIME type of the response | |
| 63 | +| `SetStream([Stream]$Stream)` | Sets the value of \$ResponseStream | |
| 64 | +| `GetContentType([string]$Path)` | Gets the content type of a file given a filepath. | |
| 65 | + |
| 66 | +# SEE ALSO |
| 67 | + |
| 68 | +about_Routing |
0 commit comments