Receiving Requests

To receive requests you need to install the Rest Server package, create a GameObject and add the RestServer component to it. This is described in the Install chapter. After the install the rest server is start up default on “localhost:8080” and can receive requests.

The rest server now can be extended with additional functionality, by writing custom components, as described below.

Registering an Endpoint

Endpoint, or urls, that the server shall respond to, can be registered any time before or after the server has been started. Only after an endpoint has been registered, it can be called from a possible client.

An endpoint can be registered like this:

using RestServer;
using UnityEngine;

public class Example : MonoBehaviour {
    // Reference to the RestServer instance 
    public RestServer.RestServer server;

    private void Start() {
        // Register the endpoint
        server.EndpointCollection.RegisterEndpoint(HttpMethod.GET, "/position", (request) => {
            // do work
        
            // send information back
            request.SendAsyncGetResponse();
        });
    }
}

The library provides two methods to register an endpoint:

  • void RegisterEndpoint(HttpMethod, String, Action)
  • void RegisterEndpoint(HttpMethod, Regex, Action)

The first parameter describes to which Http Verb the endpoint should respond to (GET, PUT, POST, …) while the second argument specifies the location of the endpoint. In the first overload, the string case, the endpoint is specified from the server root. So the location "/ping" will be accessible through http://localhost:8080/ping. The second overload, the Regex case, allows you to specify a regex the incoming location has to fulfill so to execute the defined Action. The Action is executed, when an incoming request matches both the HttpMethod and the string/Regex. Actions can be specified with the Lambda syntax

server.EndpointCollection.RegisterEndpoint(HttpMethod.GET, "/position", (request) => { 
    // Action
    request.SendAsyncGetResponse();
});

or with a method reference

void Start()
{
    server.EndpointCollection.RegisterEndpoint(HttpMethod.GET, "/position", Handler);
}

private void Handler(RestRequest request) {
    // Action
    request.SendAsyncGetResponse();
}

Getting Request Information

All request information is supplied to the user code in the request variable:

using RestServer;
using UnityEngine;

public class Example : MonoBehaviour {
    // Reference to the RestServer instance 
    public RestServer.RestServer server;

    private void Start() {
        server.EndpointCollection.RegisterEndpoint(HttpMethod.GET, "/position", (request) => {
            // Your code here
            
            var qp = request.QueryParameters // access ?a=b&c=d part
            
            request.HttpRequest.Cookies; // Cookies
            request.HttpRequest.Headers; // Headers            
            request.HttpRequest.Body // access the body directly
            request.JsonBody</* Type */>() // Deserialize the body as JSON
             
            request.SendAsyncGetResponse();
        });
    }
}

Sending responses

Requests are not terminated automatically, which means that the client waits for a responds until your code sends a response back. This is very important, that all code paths of an action do have a variant of the SendAsyncXXXResponse method executed.

For GET, POST, PUT, DELETE this can be done with

request.SendAsyncGetResponse();

when no additional data needs to be returned. The library provides many request.SendAsyncXXXResponse overloads for many use cases.