diff --git a/API.md b/API.md index cf8a907f5..c3b5cc76f 100644 --- a/API.md +++ b/API.md @@ -52,6 +52,8 @@ * [Notification](#Notification) * [Push message](#Push-message) * [Push error message](#Push-error-message) +* [Network](#Network) + * [Forward proxy](#Forward-proxy) * [System](#System) * [Get boot progress](#Get-boot-progress) * [Get system version](#Get-system-version) @@ -1207,6 +1209,52 @@ View API token in Settings - About, request header: `Authorization: T ``` * `id`: Message ID +## Network + +### Forward proxy + +* `/api/network/forkProxy` +* Parameters + + ```json + { + "url": "https://b3log.org/siyuan/", + "method": "GET", + "timeout": 7000, + "contentType": "text/html", + "headers": [ + { + "Cookie": "" + } + ], + "payload": {} + } + ``` + + * `url`: URL to forward + * `method`: HTTP method, default is `POST` + * `timeout`: timeout in milliseconds, default is `7000` + * `contentType`: Content-Type, default is `application/json` + * `headers`: HTTP headers + * `payload`: HTTP payload, object or string +* Return value + + ```json + { + "code": 0, + "msg": "", + "data": { + "body": "", + "contentType": "text/html", + "elapsed": 1976, + "headers": { + }, + "status": 200, + "url": "https://b3log.org/siyuan" + } + } + ``` + ## System ### Get boot progress diff --git a/API_zh_CN.md b/API_zh_CN.md index b92555012..2278b0dd8 100644 --- a/API_zh_CN.md +++ b/API_zh_CN.md @@ -52,6 +52,8 @@ * [通知](#通知) * [推送消息](#推送消息) * [推送报错消息](#推送报错消息) +* [网络](#网络) + * [正向代理](#正向代理) * [系统](#系统) * [获取启动进度](#获取启动进度) * [获取系统版本](#获取系统版本) @@ -1197,6 +1199,52 @@ ``` * `id`:消息 ID +## 网络 + +### 正向代理 + +* `/api/network/forkProxy` +* 参数 + + ```json + { + "url": "https://b3log.org/siyuan/", + "method": "GET", + "timeout": 7000, + "contentType": "text/html", + "headers": [ + { + "Cookie": "" + } + ], + "payload": {} + } + ``` + + * `url`:转发的 URL + * `method`:HTTP 方法,默认为 `GET` + * `timeout`:超时时间,单位为毫秒,默认为 `7000` 毫秒 + * `contentType`:HTTP Content-Type,默认为 `application/json` + * `headers`:HTTP 请求标头 + * `payload`:HTTP 请求体,对象或者是字符串 +* 返回值 + + ```json + { + "code": 0, + "msg": "", + "data": { + "body": "", + "contentType": "text/html", + "elapsed": 1976, + "headers": { + }, + "status": 200, + "url": "https://b3log.org/siyuan" + } + } + ``` + ## 系统 ### 获取启动进度 diff --git a/kernel/api/network.go b/kernel/api/network.go index f4854ddc2..eadfd528d 100644 --- a/kernel/api/network.go +++ b/kernel/api/network.go @@ -46,10 +46,12 @@ func forwardProxy(c *gin.Context) { return } - method := strings.ToUpper(arg["method"].(string)) - timeoutArg := arg["timeout"] + method := "POST" + if methodArg := arg["method"]; nil != methodArg { + method = strings.ToUpper(methodArg.(string)) + } timeout := 7 * 1000 - if nil != timeoutArg { + if timeoutArg := arg["timeout"]; nil != timeoutArg { timeout = int(timeoutArg.(float64)) if 1 > timeout { timeout = 7 * 1000 @@ -66,14 +68,13 @@ func forwardProxy(c *gin.Context) { } } - contentType := arg["contentType"] - if nil != contentType && "" != contentType { - request.SetHeader("Content-Type", contentType.(string)) + contentType := "application/json" + if contentTypeArg := arg["contentType"]; nil != contentTypeArg { + contentType = contentTypeArg.(string) } + request.SetHeader("Content-Type", contentType) - if "POST" == method { - request.SetBody(arg["payload"]) - } + request.SetBody(arg["payload"]) started := time.Now() resp, err := request.Send(method, destURL)