How to Implement External HTTP Callbacks

You can make HTTP calls into the GameSparks platform either from external servers or from your own processes. When these calls are made, you can execute some custom Cloud Code to do whatever processing you need:

Creating Cloud Code to Handle Requests

To create a script to handle the request from the Cloud Code page, under Scripts select System, and use Callback URL.

The Callback URL

https://{apiKey}.{stage}.gamesparks.net/callback/{apiKey}/{serverSecret}/{playerId}

Trailing Slash Required! If you don't pass a playerId, you still need to include the / after your serverSecret.

Adding Parameters

For a GET request, you can add parameters to the query string that are passed to your script. For example:

https://{apiKey}.{stage}.gamesparks.net/callback/{apiKey}/{serverSecret}/{playerId}?attr1=abc&attr2=bcd&attr2=cde

With this URL, the following code can be used to access these parameters:


    //attribute1 is a string
    var attribute1 = Spark.getData().attr1;

    //attribute2 is an array
    var attribute2 = Spark.getData().attr2;

    //attribute2[0] is bcd
    //attribute2[1] is cde

Producing Response Body

Using Script Data to Send Back JSON Response

The scriptData object from the callback script is written to the output stream as a JSON document.


    Spark.setScriptData("key1", "value1");
    Spark.setScriptData("key2", "value2");

This will generate the JSON output:


    {
        "key1": "value1",
        "key2": "value2"
    }

Using RESPONSE_RAW for Text-Based Output

If you do not want to write JSON but want to use your own text-based output, setting a value with the key "RESPONSE_RAW" in script data will override the JSON output and return this value directly;


    Spark.setScriptData("RESPONSE_RAW", "OK");

Will generate the response as :


    OK

Note: If you use setScriptData and not RESPONSE_RAW, then RESPONSE_HEADERS is a reserved word so it won't appear in the JSON output.

Producing Headers

Setting Response Headers

You can set response headers in the output:


Spark.setScriptData("RESPONSE_HEADERS", {"myHeader":"myValue"})

Other ways to set ResponseHeaders:

You can specify the Content-Type Response Header by passing in the following parameters: contentType and characterEncoding like this:

https://{apiKey}.{stage}.gamesparks.net/callback/{apiKey}/{serverSecret}/{playerId}?contentType=yourContentType&characterEncoding=UTF8

If these parameters are passed in but the Content-Type is also passed-in in the RESPONSE_HEADERS, the RESPONSE_HEADERS will be used.

Reading POST Data

There are instances where a remote server may not set the content-type header correctly, and the post data are not automatically translated into Spark.getData(). In these cases, the POST requests have an additional "REQUEST_BODY" attribute set into Spark.getData(). This is the body of the HTTP post request as a string, so you can do your own parsing of this value:

    var rawPostData = Spark.getData().REQUEST_BODY

Did this page help you? Please enter your feedback below. For questions about using this part of the platform, please contact support here