Endpoints
Endpoint configuration for Universal APIs.
Endpoints are defined by their URI and HTTP method. Calls made to the Universal server that match the API endpoint and method that you define will execute the API endpoint script.
1
New-PSUEndpoint -Url '/endpoint' -Method 'GET' -Endpoint {
2
"Hello, world!"
3
}
Copied!
To invoke the above method, you could use Invoke-RestMethod.
1
Invoke-RestMethod http://localhost:5000/endpoint
Copied!

Variable URL

URLs can contain variable segments. You can denote a variable segment using a colon (:). For example, the following URL would provide a variable for the ID of the user. The $Id variable will be defined within the endpoint when it is executed. Variables must be unique in the same endpoint URL.
1
New-PSUEndpoint -Url '/user/:id' -Method 'GET' -Endpoint {
2
Get-User -Id $Id
3
}
Copied!
To call this API and specify the ID, you would do the following.
1
Invoke-RestMethod http://localhost:5000/user/123
Copied!

Query String Parameters

Query string parameters are automatically passed into endpoints as variables that you can then access. For example, if you had an endpoint that expected an $Id variable, it could be provided via the query string.
1
New-PSUEndpoint -Url '/user' -Method 'GET' -Endpoint {
2
Get-User -Id $Id
3
}
Copied!
The resulting Invoke-RestMethod call must then include the query string parameter.
1
Invoke-RestMethod http://localhost:5000/user?Id=123
Copied!

Body

To access a request body, you will simply access the $Body variable. Universal $Body variable will be a string. If you expect JSON, you should use ConvertFrom-Json.
1
New-PSUEndpoint -Url '/user' -Method Post -Endpoint {
2
$User = ConvertFrom-Json $Body
3
New-User $User
4
}
Copied!
To call the above endpoint, you would have to specify the body of Invoke-RestMethod.
1
Invoke-RestMethod http://localhost:5000/user -Method Post -Body "{'username': 'adam'}"
Copied!

Form Data

You can pass data to an endpoint as form data. Form data will be passed into your endpoint as parameters.
1
New-PSUEndpoint -Url '/user' -Method Post -Endpoint {
2
param([Parameter(Mandatory)]$userName, $FirstName, $LastName)
3
4
New-User $UserName -FirstName $FirstName -LastName $LastName
5
}
Copied!
You can then use a hashtable with Invoke-RestMethod to pass form data.
1
Invoke-RestMethod http://localhost:5000/user -Method Post -Body @{
2
UserName = "adriscoll"
3
FirstName = "Adam"
4
LastName = "Driscoll"
5
}
Copied!

Param Block

You can use a param block within your script to enforce mandatory parameters and provide default values for optional parameters such as query string parameters. Variables such as $Body, $Headers and $User are provided automatically.
In the below example, the $Name parameter is mandatory and the $Role parameter has a default value of Default.
1
New-PSUEndpoint -Url '/user/:name' -Endpoint {
2
param([Parameter(Mandatory)$Name, $Role = "Default")
3
}
Copied!

Returning Data

Data returned from endpoints will be assumed to be JSON data. If you return an object from the endpoint script block, it will be automatically serialized to JSON. If you want to return another type of data, you can return a string formatted however you chose.

Processing Files

Uploading Files

You can process uploaded files by using the $Data parameter to access the byte array of data uploaded to the endpoint.
1
New-PSUEndpoint -Url '/file' -Method Post -Endpoint {
2
$Data
3
}
4
5
PS C:\Users\adamr> iwr http://localhost:5000/file -method post -InFile '.\Desktop\add-dashboard.png'
6
7
StatusCode : 200
8
StatusDescription : OK
9
Content : [137,80,78,71,13,10,26,10,0,0,0,13,73,72,68,82,0,0,2,17,0,0,1,92,8,2,0,0,0,249,210,123,106,0,0,0,1,
10
115,82,71,66,0,174,206,28,233,0,0,0,4,103,65,77,65,0,0,177,143,11,252,97,5,0,0,0,9,112,72,89,115,0,
11
0,…
Copied!
You could also save the file into a directory.
1
New-PSUEndpoint -Url '/file' -Method Post -Endpoint {
2
[IO.File]::WriteAllBytes("tempfile.dat", $Data)
3
}
Copied!

Downloading Files

You can send files down using the New-PSUApiResponse cmdlet.
1
New-PSUEndpoint -Url '/image' -Endpoint {
2
$ImageData = [IO.File]::ReadAllBytes("image.jpeg")
3
New-PSUApiResponse -ContentType 'image/jpg' -Data $ImageData
4
}
Copied!

Returning Custom Responses

You can return custom responses from endpoints by using the New-PSUApiResponse cmdlet in your endpoint. This cmdlet allows you to set the status code, content type and even specify the byte[] data for the content to be returned.
1
New-PSUEndpoint -Url '/file' -Method Get -Endpoint {
2
New-PSUApiResponse -StatusCode 410
3
}
Copied!
You can also return custom body data by using the -Body parameter of New-PSUApiResponse.
1
New-PSUEndpoint -Url '/file' -Method Get -Endpoint {
2
New-PSUApiResponse -Body "Not what you're looking for." -StatusCode 404
3
}
Copied!
Invoking the REST method will return the custom error code.
1
PS C:\Users\adamr\Desktop> invoke-restmethod http://localhost:8080/file
2
3
Invoke-RestMethod: Not what you're looking for.
Copied!
You can control the content type of the data that is returned by using the -ContentType parameter.
1
New-PSUEndpoint -Url '/file' -Method Get -Endpoint {
2
New-PSUApiResponse -Body "<xml><node>1</node><node2>2</node2></xml>" -ContentType 'text/xml'
3
}
Copied!

Persistent Runspaces

Persistent runspaces allow you to maintain runspace state between API calls. This is important for users that perform some sort of initialization within their endpoints that they do not want to execute on subsequent API calls.
By default, runspaces will be reset after each execution. This will cause variables, modules and functions defined during the execution of the API to be removed.
To enable persistent runspaces, you will need to configure an environment for your API. Set the -PersistentRunspace parameter to enable this feature. This is configured in the environments.ps1 script.
1
New-PSUEnvironment -Name 'Env' -Path 'powershell.exe' -PersistentRunspace
Copied!
You can then assign the API environment in the settings.ps1 script.
1
Set-PSUSetting -ApiEnvironment 'Env'
Copied!

Related Cmdlets

Last modified 5mo ago