Sending Responses

After receiving a request the rest server can respond in various ways to the caller. The following example will send a response with an empty header and body and with status code 200 back to the caller.

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
            
            request.CreateResponse().SendAsync();
        });
    }
}

Multiple CreateResponse() calls are possible in one request, but only one call to SendAsync() is allowed. Note that every call to CreateResponse() creates a new response object and only the object for which SendAsync() is called on determines the response.

Body/BodyJson

Body can be provided as string, byte[] or object. Objects will be converted to JSON with the Unity function JsonUtility.ToJson().

String variants:

request.CreateResponse()
        .Body("text content")
        .SendAsync();

request.CreateResponse()
        .Body("text content", 
            /* Default Content-Type */ MimeType.TEXT_PLAIN_UTF_8)
        .SendAsync();

Byte arrays or binary content:

request.CreateResponse()
        .Body(new byte[]{0xAF, 0xFE, 0x10, 0x13})
        .SendAsync();
        
request.CreateResponse()
        .Body(Encoding.UTF8.GetBytes("blah"), 
            /* Default Content-Type */ MimeType.APPLICATION_OCTET_STREAM)
        .SendAsync();

See this FAQ on how to receive binary data. Some of these methods can also be adapted to send binary data back to the caller.

JSON Response:

request.CreateResponse()
        .BodyJson(customObject)
        .SendAsync();
        
request.CreateResponse()
        .BodyJson(customObject, 
            /* Default Content-Type */ MimeType.APPLICATION_JSON_UTF_8)
        .SendAsync();

Status Code

The default status code returned is 200. This can be modified easily.

request.CreateResponse()
       .Status(201)
       .SendAsync();

This will send a status code of 500 by default

request.CreateResponse()
       .StatusError() // = 500
       .SendAsync();

Header

Headers can be provided by calling the Header() or Headers() function.

request.CreateResponse()
       .Header(HttpHeader.CONTENT_TYPE, MimeType.TEXT_PLAIN_UTF_8)
       .SendAsync();

Note that some functions, like Body() overwrite the Content-Type Header. If an alternative value needs to be specified the override of Body() can be used, or the Header() call must come last.

The HeaderBuilder helper class can be used as well.

request.CreateResponse()
        .Header(HttpHeader.CONTENT_TYPE, MimeType.TEXT_PLAIN_UTF_8)
        .SendAsync();

request.CreateResponse()
        .Headers(new HeaderBuilder(HttpHeader.ALLOW, "custom-value2"))
        .SendAsync();

Sending the response

Each CreateResponse() creates a response and this must be terminated with SendAsync(). The SendAsync() method returns a boolean to indicate, if the message has been enqueued correctly.

Shortcut Methods

The Response builder provides some methods for common use cases.

InternalServerError() sets the status code to 500 and the body to “Internal server error”.

request.CreateResponse()
        .InternalServerError()
        .SendAsync();
        
request.CreateResponse()
        .InternalServerError("Custom error message")
        .SendAsync();

NotFound() sets the status code to 404 and the message to No endpoint found for <url> and Method <method>.

request.CreateResponse()
        .NotFound() // default message: No endpoint found for <url> and Method <method>
        .SendAsync();

request.CreateResponse()
        .NotFound("Custom error message")
        .SendAsync();

NotAuthenticated() sets the status code to 401 and the Authentication header.

request.CreateResponse()
        .NotAuthenticated() // Basic realm
        .SendAsync();
        
request.CreateResponse()
        .NotAuthenticated("custom realm")
        .SendAsync();