Skip to content

httpClient

HTTP 客户端,用于在脚本中发送请求。支持在 API 导出或其他操作期间向外部服务发送 HTTP 请求。

方法

方法返回类型说明
httpClient.get(url)HttpResponse发送 GET 请求
httpClient.post(url, body)HttpResponse发送 POST 请求
httpClient.put(url, body)HttpResponse发送 PUT 请求
httpClient.delete(url)HttpResponse发送 DELETE 请求

响应对象

httpClient 方法返回的响应对象提供以下访问:

属性/方法返回类型说明
response.code()IntHTTP 状态码
response.body()String响应体字符串
response.headers()Map<String, String>响应头

子页面

  • request - http.call.before/http.call.after 中可用的 HTTP 请求包装对象
  • response - http.call.after 中可用的 HTTP 响应包装对象

示例

GET 请求

properties
http.call.before=groovy:```
def response = httpClient.get("http://config-server/api/config")
if (response.code() == 200) {
    def config = new JsonSlurper().parseText(response.body())
    logger.info("Config loaded: " + config)
}
```

带 JSON 的 POST 请求

properties
export.before=groovy:```
import groovy.json.JsonOutput

def data = [
    project: "my-project",
    apis: []
]

def json = JsonOutput.toJson(data)
def response = httpClient.post("http://api-server/sync", json)
logger.info("Sync result: " + response.code())
```

带令牌缓存的认证

properties
http.call.before=groovy:```
def token = session.get("auth_token")
if (!token) {
    def loginResponse = httpClient.post("http://auth-server/login", [
        username: config.get("auth.username"),
        password: config.get("auth.password")
    ])

    if (loginResponse.code() == 200) {
        def result = new JsonSlurper().parseText(loginResponse.body())
        token = result.token
        session.set("auth_token", token)
    }
}

if (token) {
    logger.info("Using token for request to: " + request.url())
}
```

条件请求

properties
http.call.before=groovy:```
def apiUrl = config.get("api.url")
if (apiUrl) {
    def healthCheck = httpClient.get(apiUrl + "/health")
    if (healthCheck.code() != 200) {
        logger.warn("API server health check failed")
    }
}
```

注意事项

  • HTTP 请求是同步的 - 它们会阻塞直到完成
  • 适当时使用 sessionlocalStorage 缓存响应
  • 使用状态码检查优雅地处理错误
  • httpClient 根据 IDE 设置配置(Apache HttpClient、Java URLConnection 或 IntelliJ HTTP Client)

相关链接

  • request - http.call.before/http.call.after 中的请求包装对象
  • response - http.call.after 中的响应包装对象
  • session - 用于缓存的会话级存储
  • localStorage - 用于缓存的持久化存储
  • config - 配置访问
  • logger - 日志工具

基于 Apache-2.0 许可发布