如何使用Oracle PLSQL评估以HATEOAS格式返回的某些URL

i5desfxk  于 2023-10-16  发布在  Oracle
关注(0)|答案(1)|浏览(105)

例如:我从GET服务返回了如下响应。我想评估URL:URL2.com和URL7.com中的一个或多个,并存储结果。做这件事的好方法是什么。我知道使用UTL_HTTP进行GET调用的方法,但在这种情况下,我应该对'parent' URL进行GET调用,然后对所有必需的子请求进行进一步的GET调用吗?

{
    "items": [
        {
            "PersonId": 1,
            "PersonNumber": "2",
            "CorrespondenceLanguage": null,
            "BloodType": null,
            "DateOfBirth": "1970-03-11",
            "DateOfDeath": null,
            "CountryOfBirth": null,
            "RegionOfBirth": null,
            "TownOfBirth": null,
            "ApplicantNumber": null,
            "CreatedBy": "",
            "CreationDate": "2018-06-21T03:46:31.499+00:00",
            "LastUpdatedBy": "",
            "LastUpdateDate": "2021-05-25T10:52:05.751+00:00",
            "links": [
                {
                    "rel": "self",
                    "href": "URL1.com",
                    "name": "workers",
                    "kind": "item",
                    "properties": {
                        "changeIndicator": "X"
                    }
                },
                {
                    "rel": "canonical",
                    "href": "URL2.com",
                    "name": "workers",
                    "kind": "item"
                },
                {
                    "rel": "child",
                    "href": "URL3.com",
                    "name": "addresses",
                    "kind": "collection"
                }
            ]
        },
        {
            "PersonId": 2,
            "PersonNumber": "1",
            "CorrespondenceLanguage": null,
            "BloodType": null,
            "DateOfBirth": "1969-07-04",
            "DateOfDeath": null,
            "CountryOfBirth": null,
            "RegionOfBirth": null,
            "TownOfBirth": null,
            "ApplicantNumber": null,
            "CreatedBy": "X.X",
            "CreationDate": "2020-03-06T14:05:09+00:00",
            "LastUpdatedBy": "",
            "LastUpdateDate": "2021-04-26T23:01:31.867+00:00",
            "links": [
                {
                    "rel": "self",
                    "href": "URL5.com",
                    "name": "workers",
                    "kind": "item",
                    "properties": {
                        "changeIndicator": "X"
                    }
                },
                {
                    "rel": "canonical",
                    "href": "URL6.com",
                    "name": "workers",
                    "kind": "item"
                },
                {
                    "rel": "child",
                    "href": "URL7.com",
                    "name": "addresses",
                    "kind": "collection"
                }
            ]
        }
    ],
    "count": 25,
    "hasMore": true,
    "limit": 25,
    "offset": 0,
    "links": [
        {
            "rel": "self",
            "href": "URL8",
            "name": "workers",
            "kind": "collection"
        }
    ]
}
zmeyuzjn

zmeyuzjn1#

要使用Oracle PL/SQL评估以HATEOAS格式返回的URL,可以执行以下步骤:
1.解析JSON响应。
1.遍历URL
1.发出GET请求
1.处理响应
下面是一个示例PL/SQL脚本,可以帮助您入门:

DECLARE
    l_response CLOB;
    l_json_obj APEX_JSON.T_VALUES;
    l_url VARCHAR2(4000); -- Adjust the length as needed
BEGIN
    -- Assuming l_response contains your JSON response
    -- Parse the JSON response
    APEX_JSON.PARSE(l_response);
    
    -- Iterate through the "items" array
    FOR i IN 1 .. APEX_JSON.get_count(p_path => 'items') LOOP
        -- Get the "links" array for each item
        APEX_JSON.get_values(p_values => l_json_obj, p_path => 'items[%d].links', p0 => i);
        
        -- Iterate through the "links" array
        FOR j IN 1 .. APEX_JSON.get_count(p_values => l_json_obj) LOOP
            -- Get the URL
            APEX_JSON.get_varchar2(p_values => l_json_obj, p_path => 'href', p0 => j, p_out => l_url);
            
            -- Check if the URL is one you want to evaluate
            IF l_url = 'URL2.com' OR l_url = 'URL7.com' THEN
                -- Make a GET request to the URL using UTL_HTTP
                -- Process the response and store the result as needed
                -- You can use UTL_HTTP.REQUEST and UTL_HTTP.RESPONSE functions here
            END IF;
        END LOOP;
    END LOOP;
    
    -- Clean up after parsing
    APEX_JSON.FREE_PARSE;
END;
/

此脚本将帮助您解析JSON响应并从“links”数组中提取URL,并向您感兴趣的URL发出GET请求。您需要根据您的特定需求添加错误处理和进一步处理。

相关问题