Service Communication
Service Communication
This document describes the communication methods GM Web SDK supports, so your app can interact efficiently with GM backend services and the system.
Overview
GM provides several communication capabilities, including HTTP communication with backend services, proxy forwarding and system command execution.
Prerequisites
Important
A micro-app that includes a backend service must wait for $gm.init() to succeed before calling /api/call/{developer-name}/{app-name}/{method}.
$gm.init().then(() => {
console.log('应用服务状态检查成功');
// Send app service requests here
}).catch(error => {
console.error('应用服务初始化失败:', error);
});For details, see SDK Initialization.
HTTP communication
request
request is an HTTP client instance wrapped around Axios, dedicated to communicating with GM backend services.
Type definition:
| Property | Type | Description |
|---|---|---|
| request | AxiosInstance | Axios instance that supports every Axios configuration option |
Example:
// GET request
$gm.request.get('/api/user/profile')
.then(response => {
console.log('用户信息:', response.data);
})
.catch(error => {
console.error('请求失败:', error);
});
// POST request
$gm.request.post('/api/files/get_dir', {
path: '/',
search: '',
all: false,
showRow: 9999
}).then(response => {
console.log('目录信息:', response.data);
}).catch(error => {
console.error('获取目录失败:', error);
});
// Use a configuration object
$gm.request({
url: '/api/data',
method: 'PUT',
data: { id: 1, name: 'updated' },
timeout: 5000,
headers: {
'Content-Type': 'application/json'
}
}).then(response => {
console.log('更新成功:', response.data);
}).catch(error => {
console.error('更新失败:', error);
});apiForward
apiForward provides proxy forwarding. It lets you reach external API services through the GM proxy, which solves cross-origin problems and keeps external requests in one place.
Parameters:
| Parameter | Type | Required | Description |
|---|---|---|---|
| url | string | ✅ | Target API URL |
| options.method | string | ✅ | HTTP request method |
| options.headers | object | ❌ | Request headers |
| options.body | any | ❌ | Request body data |
| options.timeout | number | ❌ | Timeout in milliseconds, defaults to 30000 |
Example:
// Basic GET request
$gm.apiForward('https://api.example.com/users', {
method: 'GET'
}).then(response => {
console.log('用户列表:', response);
}).catch(error => {
console.error('请求失败:', error);
});
// POST request with JSON data
$gm.apiForward('https://api.example.com/users', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': 'Bearer your-token'
},
body: {
name: '张三',
email: 'zhangsan@example.com'
},
timeout: 10000
}).then(response => {
console.log('创建成功:', response);
}).catch(error => {
console.error('创建失败:', error);
});System command execution
execShell
execShell lets you run system shell commands directly from the frontend, for scenarios that need deep interaction with the system.
Security
Running shell commands carries security risks. Make sure a command comes from a trusted source and avoid running unvalidated input from users.
Parameters:
| Parameter | Type | Required | Description |
|---|---|---|---|
| cmd | string | ✅ | Shell command to run |
| timeout | number | ❌ | Timeout in seconds |
| cwd | string | ❌ | Directory to run the command in; defaults to the current working directory |
| name | string | ❌ | User name to run as (specific environments only) |
Example:
// Basic command execution
$gm.execShell({
cmd: 'ls -la'
}).then(result => {
console.log('命令输出:', result.stdout);
console.log('退出码:', result.exitCode);
if (result.stderr) {
console.warn('错误输出:', result.stderr);
}
}).catch(error => {
console.error('命令执行失败:', error);
});
// Run a command in a specific directory
$gm.execShell({
cmd: 'npm install',
cwd: '/path/to/project',
timeout: 60000 // 1 minute timeout
}).then(result => {
if (result.exitCode === 0) {
console.log('npm install 成功完成');
} else {
console.error('npm install 失败:', result.stderr);
}
}).catch(error => {
console.error('命令执行失败:', error);
});