Skip to content

request

HTTP 请求对象,在 http.call.beforehttp.call.after 规则脚本中可用。

request 对象提供对即将发送的 HTTP 请求属性的只读访问。

可用性

规则变量
http.call.beforerequest
http.call.afterrequest

方法

方法返回类型说明
request.url()String请求 URL
request.method()StringHTTP 方法(GET、POST 等)
request.headers()List<KeyValue>请求头
request.query()List<KeyValue>查询参数
request.body()String?请求体(可空)
request.formParams()List<FormParam>表单参数
request.cookies()List<HttpCookie>Cookie
request.contentType()String?Content-Type 头的值(可空)

示例

查看请求详情

properties
http.call.before=groovy:```
logger.info("Sending " + request.method() + " " + request.url())
if (request.body() != null) {
    logger.info("Body: " + request.body())
}
if (!request.formParams().isEmpty()) {
    logger.info("Form: " + request.formParams())
}
if (!request.query().isEmpty()) {
    logger.info("Query: " + request.query())
}
```

检查请求方法

properties
http.call.before=groovy:```
if (request.method() == "POST") {
    logger.info("POST body: " + request.body())
}
```

读取请求头

properties
http.call.before=groovy:```
def headers = request.headers()
headers.each { h ->
    logger.info(h.key() + ": " + h.value())
}
```

相关链接

基于 Apache-2.0 许可发布