# SWebService

## Set up your web service

To setup your server for communication with space, in the root of your domain, on the port you are using, place a file named 'sinewave.space.scripting.txt' containing 'SPACE\_OK'. E.g. [http://somewhere.com/sinewave.space.scripting.txt](http://somewhere.com/sinewave.space.scripting.txt) - if this file is not present, you will be unable to use scripting to communicate with the domain. _Note: you should use HTTPS for all API calls if you want these to work reliably in WebGL. You may also need to implement a CORS policy in your webserver headers._

## Index

### Functions Index

| Function Name                                                                                                 |
| ------------------------------------------------------------------------------------------------------------- |
| void [**Get**](#get) (string url, Closure onComplete, Table headers=null, float timeout=0)                    |
| void [**Post** ](#post)(string url, string data, Closure onComplete, Table headers=null, float timeout=0)     |
| SResource [**GetImage** ](#getimage)(string url, Closure onComplete=null, Table header=null, float timeout=0) |

## Functions

### Get

void **Get** (string url, Closure onComplete, Table headers=null, float timeout=0)

_Performs a HTTP\[S] GET against URL and returns the contents as a SWebResponse_

| Parameter | Type | Description |
| --------- | ---- | ----------- |
|           |      |             |

{% tabs %}
{% tab title="Lua" %}
```lua
function OnResponseFunction(responseData)
--
end
Space.WebServices.Get('A URL', OnResponseFunction)
```
{% endtab %}
{% endtabs %}

{% tabs %}
{% tab title="Lua" %}
```lua
-- Example: Retrieve user-specific data from your integration endpoint

local userID = "User123"       -- Typically retrieved dynamically, e.g., from Space.Scene.CurrentUser
local serviceID = "CRMApp"     -- Identifier for the integrated service or application
local sentData = "http://www.yourdomain.net/getIntegrationData.php?id=" .. userID .. "&service=" .. serviceID

-- Callback function to handle the API response
local response = function(data)
  if data.Error == nil then
    Space.Log(data.Response)  -- Log the successful response
  else 
    Space.Log(data.Error)     -- Log any error returned by the API
  end
end

-- Make a GET request to your integration API
Space.WebServices.Get(sentData, response)

-- Parse the response to extract and use relevant integration data

```
{% endtab %}
{% endtabs %}

### Post

void **Post** (string url, string data, Closure onComplete, Table headers=null, float timeout=0)

_Performs a HTTP\[S] POST against URL using data as a post string and returns the contents as a SWebResponse_

| Parameter | Type | Description |
| --------- | ---- | ----------- |
|           |      |             |



{% tabs %}
{% tab title="Lua" %}
```lua
function OnResponseFunction(responseData)
--
end
Space.WebServices.Post('A URL', OnResponseFunction)
```
{% endtab %}
{% endtabs %}

{% tabs %}
{% tab title="Lua" %}
```lua
local userID = "AvatarID"
local gameID = "GameID"
local url = "http://www.yourdomain.net/getGameData.php"
local sentData = "id=" .. userID .. "&game=" .. gameID

local response = function(data)
  if data.Error == nil then
    Space.Log(data.Response)
  else 
    Space.Log(data.Error)
  end
end

Space.WebServices.Post(url, sentData, response)
-- Like the example above, just using http(s) post method.
```
{% endtab %}
{% endtabs %}

### GetImage

[SResource](../types/sresource) **GetImage** (string url, Closure onComplete=null, Table header=null, float timeout=0)

_Returns a valid_ [_SResource_](../types/sresource) _for a image on a remote domain that can be used via e.g._ [_SMaterial_](../types/smaterial)_. While the image loads, it will be a white pixel that will be substituted with the real image once loaded._

| Parameter | Type | Description |
| --------- | ---- | ----------- |
|           |      |             |



{% tabs %}
{% tab title="Lua" %}
```lua
resourceImage = Space.WebServices.GetImage('An Image URL')
```
{% endtab %}
{% endtabs %}

{% tabs %}
{% tab title="Lua" %}
```lua
 image = "mrlee.jpg"
 server = "https://middleware.systems/"
 obj = Space.Host.GetReference("dispobj")
 
 resrc = Space.WebServices.GetImage(server .. "mrlee.jpg")
 obj.Renderer.Material.SetTexture("_MainTex", resrc)
```
{% endtab %}
{% endtabs %}
