requests 使用文档

快速入门

requests 常用语法示例

事先准备的变量 helloworld 内容(注:helloworld 变量要么是字符串,要么是 JSON 对象)

{
    "sData1": "xxxsData1",
    "hello": "hello world"
}

requests 模板

[{
    "headers": {
        "Accept": "application/json",
        "Content-Type": "application/json",
        "hello": "${<helloWorld>$.hello}" // 标注(1)
    },
    "method": "GET",
    "body": [
        "${helloWorld}", // 标注(2)
        "${helloWorld}|json", // 标注(3)
        "xiaowang",
        "ming", {
            "keys": "${<helloWorld>$}|json", //标注(4)
            "unfoldJson": "${...helloWorld}", //标注(5)
            "a": "${...helloWorld}" //标注(6)
        }
    ],
    "url": "${dataUrl}/backup/${<helloWorld>$.hello}"
}]

以上标注说明和预期效果

标注(1):JSONPath 语法,取 helloWorld 变量里的 hello 字段值,运算后的结果为

[{
    "headers": {
        "Accept": "application/json",
        "Content-Type": "application/json",
        "hello": "hello world"
    },
    // ......
}]

标注(2):helloWorld 变量整个内容植入(以字符串的方式),运算后的结果为

[{
    //......
    "method": "GET",
    "body": [
        "{\"sData1\":\"xxxsData1\",\"hello\":\"hello world\"}",
        "${helloWorld}|json",
        "xiaowang",
        "ming", {
            // ......
        }
    ],
    "url": "${dataUrl}/backup/${<helloWorld>$.hello}"
}]

标注(3):helloWorld 变量整个内容植入(以 JSON 的方式),运算后的结果为

[{
    //......
    "body": [
        "${helloWorld}",
        {"sData1":"xxxsData1","hello":"hello world"},
        "xiaowang",
        "ming", {
            // ......
        }
    ],
    "url": "${dataUrl}/backup/${<helloWorld>$.hello}"
}]

标注(4):JSONPath 取 helloWorld 变量内容植入(以 JSON 的方式),运算后的结果为

[{
    // ......
    "body": [
        // ......
        "ming", {
            "keys": {"sData1":"xxxsData1","hello":"hello world"},
            "unfoldJson": "${...helloWorld}",
            "a": "${...helloWorld}"
        }
    ],
    "url": "${dataUrl}/backup/${<helloWorld>$.hello}"
}]

标注(5):当出现这样的字符和这样的值时 "unfoldJson": "${...helloWorld}", 语义是展开 helloWorld 变量内的所有字段,运算后的结果为

[{
    // ......
    "body": [
        // ......
        "ming", {
            "keys": "${<helloWorld>$}|json",
            "sData1": "xxxsData1",
            "hello": "hello world"
            "a": "${...helloWorld}"
        }
    ],
    "url": "${dataUrl}/backup/${<helloWorld>$.hello}"
}]

标注(6):当存在 ${...helloWorld} 的值时,helloWorld 变量整个内容植入(以 JSON 的方式),与 ${helloWorld} | json 语义一致,运算后的结果为

[{
    // ......
    "body": [
        // ......
        "ming", {
            "keys": "${<helloWorld>$}|json",
            "unfoldJson": "${...helloWorld}",
            "a": {"sData1":"xxxsData1","hello":"hello world"}
        }
    ],
    "url": "${dataUrl}/backup/${<helloWorld>$.hello}"
}]

requests 服务编排示例

requests 多个请求,上一个请求的值如何传递到下一个请求

假设第一个请求的响应内容

{
    "sData1": "xxxsData1",
    "hello": "hello world"
}

requests 模板

[   //第一个请求
    {
        "method": "GET",
        "url": "http://example.com/getHelloworld",
        "headers": {
            "Accept": "application/json",
            "Content-Type": "application/json"
        },
        "respEnvName": "ENV_VALUE" //该请求Response结果作为变量的命名,可为空。根据该命名可在其他环节进行变量引用,如:下一个request,或者是下下个request
    },
    //第二个请求
    {
        "method": "POST",
        "url": "http://example.com/save/helloworld?hello=${$.hello}", // 标注(1)
        "body": { 
            "hello": "${<ENV_VALUE>$.hello}", // 标注(2)
            "length": {
                "width": 100,
                "height": 100
            },
            "content1": "${$}|json", // 标注(3)
            "content2": "${ENV_VALUE}|json" // 标注(4)
        },
        "headers": {
            "Accept": "application/json",
            "Content-Type": "application/json",
            "X-Credential-Token": "${UAA_CREDENTIAL_TOKEN}" // 标注(5)
        }
    }
]

以上标注说明和预期效果

标注(1):从上一个请求响应里获取其 hello 字段,运算后的结果为

[
    // ......
    {
        "method": "POST",
        "url": "http://example.com/save/helloworld?hello=hello world",
        // ......
    }
]

标注(2):从前面请求中的 respEnvName=ENV_VALUE 的请求响应里获取 hello 字段内容,运算后的结果为

[
    // ......
    {
        // ......
        "body": { 
            "hello": "hello world",
            "length": {
                "width": 100,
                "height": 100
            },
            // ......
        },
        // ......
    }
]

标注(3):获取上一个请求的响应内容并且以 JSON 形式植入,运算后的结果为

[
    // ......
    {
        // ......
        "body": { 
            "hello": "${<ENV_VALUE>$.hello}",
            "length": {
                "width": 100,
                "height": 100
            },
            "content1": {"sData1":"xxxsData1","hello":"hello world"},
            "content2": "${ENV_VALUE}|json"
        },
        // ......
    }
]

标注(4):从前面请求中的 respEnvName=ENV_VALUE 的请求响应结果以 JSON 方式植入,运算后的结果为

[
    // ......
    {
        // ......
        "body": { 
            "hello": "${<ENV_VALUE>$.hello}",
            "length": {
                "width": 100,
                "height": 100
            },
            "content1": ${$}|json",
            "content2": {"sData1":"xxxsData1","hello":"hello world"}
        },
        // ......
    }
]

标注(5):从 pod 容器内获取环境变量UAA_CREDENTIAL_TOKEN(假定值为 sfsdfsdf222)的值,并植入,运算后的结果为

[
    // ......
    {
        // ......
        "headers": {
            "Accept": "application/json",
            "Content-Type": "application/json",
            "X-Credential-Token": "sfsdfsdf222"
        }
    }
]

form 表单提交和响应头 header 返回

requests 新增能力,form 表单提交和响应头 header 返回

[{
    "headers": {
        "name": "chensc"
    },
    "method": "POST",
    "body": {
        "username": "system",
        "password": ":pd:NFZMXUa=TiD41ww=:pd:"
    },
    "formCommit": true, // 是否使用form表单提交,如果true body将以表单的形式提交,可为空,没有该字段将走老模式
    "responseHeader": true, // 是否响应头header,可为空,没有该字段将走老模式
    "url": "service://entry/login"
}, {
    "headers": {
        "name": "chensc"
    },
    "method": "POST",
    "body": {
        "resp_headers": "${$.headers}|json", //获取第一个请求的响应头
        "resp_datas": "${$.datas}|json" //获取第一个请求的响应结果
    },
    "url": "service://jobswork/main/workHandleStr"
}]

上述第一个请求的返回结果格式(前提是 responseHeader=true)

{
    "datas": {},
    "headers": {}
}

requests java 基础包

引入依赖

    <dependency>
        <groupId>com.justep</groupId>
        <artifactId>requests</artifactId>
        <version>1.0.0</version>
    </dependency>

导入 com.justep.util.requet.RequestUtil 类进行使用

    /**
    * 注:如果远程调用发生NetHttpInvokeHandlerException异常时,那么返回结果会被Results接管
    * @param requests 请求模版,JSONArray类型
    * @param vars 变量
    * @param extHeaders 扩展透传头
    * @param options 可选项,一般里面包含timeoutSeconds字段
    * @return
    * @throws RequestException 异常处理类
    */
    public static HttpInvokeResult<JSONObject> invoke(JSONArray requests,
        Map<String, Object> vars,
        Map<String, String> extHeaders,
        Map<String, Object> options) throws RequestException 

    /**
     * 注:如果远程调用发生NetHttpInvokeHandlerException异常时,那么返回结果会被Results接管
     * @param requests 请求模版,String类型
     * @param vars 变量
     * @param extHeaders 扩展透传头
     * @param options 可选项,一般里面包含timeoutSeconds字段
     * @return
     * @throws RequestException 异常处理类
     */
    public static HttpInvokeResult<JSONObject> invoke(String requests,
        Map<String, Object> vars,
        Map<String, String> extHeaders,
        Map<String, Object> options) throws RequestException 

    public static HttpInvokeResult<JSONObject> invoke(RequestInfo requestInfo, ProvideEnvVar provideEnvVar) throws RequestException

JSONPath 语法

参考 https://www.cnblogs.com/shareToAll/p/15132438.html

https://blog.csdn.net/weixin_44791664/article/details/135295775

results matching ""

    No results matching ""