Walkthrough Example
Making a call to your web api from Blueprint
Last updated
Making a call to your web api from Blueprint
Last updated
Imagine you have a web service that has the following api:
Method + URL | Request/Response | Description |
---|---|---|
GET /api/counter | Response:
| Returns a global counter shared by every user. Requires no authentication. |
PUT /api/counter | Request:
| Updates the shared counter by adding |
GET /api/counter/my | Response:
| Returns a personal counter for the authenticated user. Required M2 Delegated token auth. |
PUT /api/counter/my | Request:
| Updates the personal counter by adding |
In the Unreal Editor, create Blueprint USTRUCT definitions for the request / response objects.
This defines the response back from your API.
This defines the request to the PUT api.
Create a Morpheus Actor with some interactable behaviour. Doing this is out of scope of this section, as it's very much dependent on what you are trying to do.
Your actor could be a control panel, an Interactable object or anything in between.
On some trigger (eg: a user pushing a button) hook up the Http Get Json node
The relevant "Update Counter Value" implementation can look as follows:
In this example, we are reading the JSON object required by the node into a variabled that's typed to the response (eg: S_HttpTaskExampleGetResponse).
You can then Break the struct to extract any data you need; for this example you can print to a debug message.
Making the PUT request is similar, but it requires you to pass a Json Object.
To do this, you can use the "Make Json Object from Struct" node to take your input and serialize it to JSON.
The result handling is the same as before.
You should be in a position to make GET and PUT requests to your counter service and get the results back.
Authenticated requests are the same process except for three small changes:
You need to set an "Auth Context"
You should ideally set a scope (eg: World)
You should tick the "Include Delegated Auth Token" option
A note on Auth Contexts. Auth Contexts determine how we pick up the identity of the actor making the request. For example, if a client is making the request via an interaction, this Auth Context will always be the Morpheus Actor (or an attached Morpheus Actor component) owned by that client. For a server, it can be the Authorative actor (assuming it is running with server authority). Auth Contexts are local to the client/server and cannot be executed remotely; eg: you cannot make a http request using the Auth Context of another actor you're not authoritative over.
Here's an example of the GET request set to use identity.
In this case, the Auth Context is the local player's actor that's provided as the Interaction Instigator.
Here's an example of the same GET but on the server.
In this example, the action is being fired via a Server RPC (eg: it's acting on the server). As a result, we can use "self" as the Auth Context because the actor itself is server authoritative.