Skip to content

Http

发送一个Http请求

业务属性

属性含义说明输入限定示例值
enable是否启用该控件,未启用时不编译是否编译选择值启用
name控件返回值命名控件输出值变量名常量httpResponse
text与控件关联的文本设计页控件显示常量http
upstream上游服务选择值upstream1
methodHTTP方法Post/GET选择值GET
uri服务路由未选择上游时请输入绝对路径常量/流程变量#{uri}
headersHTTP请求头key常量
value常量/流程变量
keyh1
value#{h1}
requestBodyHTTP请求体常量,Json/Xml数据节点可用流程变量/流程变量{"a":"#{a}"}
original原始报文默认返回对象实体选择值禁用
throwable发生异常时中断请求启用:控件执行异常时立即中断请求,并返回错误
禁用:控件执行异常时将被忽略,并继续执行其后的流程
控件底层使用http应用层传输协议
当http请求失败或http响应状态码不是200(http请求成功状态码)时,代表发生异常
选择值启用

输出值

  • 正常请求输出值

Http控件返回以属性name为名称的实体,该实体包含表示http响应结果的属性result

当属性original“启用” 时,result为http响应原始报文。

当属性original“禁用” 时,result为符合Json格式的http响应报文的反序列化实体对象。

注意:当http响应报文不符合Json格式时,result为空的实体对象

  • throwable为 “禁用” ,http请求异常时控件输出值

控件返回以属性name为名称的实体,该实体包含表示异常信息的属性err(此时属性resultnil)。

使用示例

假设有一原路返回http请求中的body参数的接口,我们使用Http控件对其发送请求。

实体对象输出

控件返回值命名为 httpResponse

method选择 POST

uri输入 http://localhost:6636/api/return

img_2.png

headers 选择 Content-Typeapplication/json

img_3.png

http请求体requestBody 输入:

json
{
  "text": "this is a text from body"
}
{
  "text": "this is a text from body"
}
img_4.png

original 属性设置为:禁用

使用Return控件对http请求的返回值进行输出:

img_9.png

Return控件的data属性输入:

json
{
  "httpResponse": "#{httpResponse}"
}
{
  "httpResponse": "#{httpResponse}"
}

使用curl请求接口:

$ curl http://localhost:6636/api/http
$ curl http://localhost:6636/api/http

接口返回:

json
{
    "httpResponse": {
        "result": {
            "text": "this is a text from body"
        }
    }
}
{
    "httpResponse": {
        "result": {
            "text": "this is a text from body"
        }
    }
}

原始报文输出

以上示例中,将http控件的original 属性设置为:启用

使用curl请求接口:

$ curl http://localhost:6636/api/http
$ curl http://localhost:6636/api/http

接口返回:

json
{
    "httpResponse": {
        "result": "{\"text\":\"this is a text from body\"}"
    }
}
{
    "httpResponse": {
        "result": "{\"text\":\"this is a text from body\"}"
    }
}

使用变量参数

我们在上述示例的基础上作修改,使用变量替代常量。

首先使用LuaScript控件定义参数

lua
local http_url = "http://localhost:6636/api/return"
local content_type = "application/json"
local request_body = { text = "this is a custom param"}
local http_url = "http://localhost:6636/api/return"
local content_type = "application/json"
local request_body = { text = "this is a custom param"}
img_12.png

修改urirequestBody

img_15.png

修改headers

img_13.png

使用curl请求接口:

$ curl http://localhost:6636/api/http
$ curl http://localhost:6636/api/http

接口返回:

json
{
    "httpResponse": {
        "result": {
            "text": "this is a custom param"
        }
    }
}
{
    "httpResponse": {
        "result": {
            "text": "this is a custom param"
        }
    }
}