# SWebservice

**成员** 2

|      |               |                            |
| ---- | ------------- | -------------------------- |
| 函数 | [Get](#get)   | Performs an HTTP\[S] GET.  |
| 函数 | [Post](#post) | Performs an HTTP\[S] POST. |

## 函数

### Get

void Get(string url, Closure onComplete, Table headers = null)

Performs an HTTP\[S] GET. The callback receives two parameters: `success` (bool) and `response` (string).

参数

| 参数       | 类型             | 说明                                                                                                      |
| ---------- | ---------------- | --------------------------------------------------------------------------------------------------------- |
| url        | string           | The absolute URL to request.                                                                              |
| onComplete | Closure          | Callback `(bool success, string response)` invoked on completion.                                         |
| headers    | Table (optional) | Supported keys: `Bearer` (maps to `Authorization: Bearer <token>`), `Content-Type` (rarely used for GET). |

返回值

`void`

示例

**Lua**

```lua
-- Simple GET
local url = "https://httpbin.org/get"
Space.WebServices.Get(url, function(success, response)
  if success then
    Space.Log("OK: " .. response)
  else
    Space.Log("Request failed: " .. response)
  end
end)

```

**Lua**

```lua
-- GET with Bearer auth
local url = "https://api.example.com/me"
local headers = { ["Bearer"] = "YOUR_ACCESS_TOKEN" }

Space.WebServices.Get(url, function(success, response)
  if success then
    Space.Log(response)
  else
    Space.Log("Error: " .. response)
  end
end, headers)

```

### Post

void Post(string url, string data, Closure onComplete, Table headers = null)

Performs an HTTP\[S] POST. The callback receives two parameters: `success` (bool) and `response` (string).

参数

| 参数       | 类型             | 说明                                                                                                                                  |
| ---------- | ---------------- | ------------------------------------------------------------------------------------------------------------------------------------- |
| url        | string           | The absolute URL to POST to.                                                                                                          |
| data       | string           | Request body string. For JSON, send a JSON string and set `Content-Type`.                                                             |
| onComplete | Closure          | Callback `(bool success, string response)` invoked on completion.                                                                     |
| headers    | Table (optional) | Supported keys: `Content-Type` (defaults to `application/x-www-form-urlencoded`), `Bearer` (maps to `Authorization: Bearer <token>`). |

返回值

`void`

示例

**Lua**

```lua
-- Form POST
local url = "https://httpbin.org/post"
local body = "x=1&y=2"
Space.WebServices.Post(url, body, function(success, response)
  if success then
    Space.Log("OK: " .. response)
  else
    Space.Log("Error: " .. response)
  end
end)

```

**Lua**

```lua
-- JSON POST with Bearer auth
local url = "https://api.example.com/widgets"
local body = '{"name":"Server Created Widget"}'
local headers = { ["Content-Type"] = "application/json", ["Bearer"] = "YOUR_ACCESS_TOKEN" }

Space.WebServices.Post(url, body, function(success, response)
  if success then
    Space.Log("Created: " .. response)
  else
    Space.Log("Error: " .. response)
  end
end, headers)

```

Note:

* Server requests automatically include: `X-Sinespace-Region-ID`, `X-Sinespace-Instance-ID`, `X-Sinespace-Script-ID`, `X-Sinespace-Runtime-Type`, and `X-Sinespace-In-Editor`.
* There is no `timeout` parameter on server web requests.
