简体   繁体   English

更新来自 API 的 JSON 响应,并使用 Rest Assured 将更新后的响应作为输入/正文传递给另一个 API

[英]Update JSON response from an API and pass the updated response as input/body to another API using Rest Assured

I am currently calling an API using Rest-Assured, whose response body is posted below.我目前正在使用 Rest-Assured 拨打 API,其响应正文发布在下方。 i need to pick a portion of the response, ie whole of WorkItems tag(at line 3), and update the fields "CorrelationUId" and "Value"(node within WorkItemAttributes tag) whose value needs to be set to a unique value.我需要选择响应的一部分,即整个 WorkItems 标签(第 3 行),并更新字段“CorrelationUId”和“Value”(WorkItemAttributes 标签内的节点),其值需要设置为唯一值。

The updated JSON will serve as a body for another API. How can i achieve this using Rest-Assured and java?更新后的 JSON 将作为另一个 API 的主体。我如何使用 Rest-Assured 和 java 来实现这一点?

   {
    "TotalRecordCount": 1,
    "BatchSize": 500,
    "WorkItems": [{
        "CreatedByApp": "IssueManagement",
        "ItemState": 1,
        "StackRank": 0,
        "CorrelationUId": "05c0df91-cd6f-4f74-8e19-0be556879e59",
        "RowStatus": null,
        "WorkItemDeliveryConstructs": [{
                "CreatedByUser": "Gateway",
                "ItemState": 0
            }

        ],

        "WorkItemLanguages": null,
        "WorkItemProductInstances": [{
            "ModifiedOn": "2020-08-05T05:01:15.335316Z",
            "UserUId": null,
            "ItemState": 0
        }],
        "WorkItemAssociations": null,
        "WorkItemAttachments": null,
        "WorkItemAttributes": [{

                "IdValue": "00000000-0000-0000-0000-000000000000",
                "IdExternalValue": "",
                "Value": "enter unique data here",
                "ItemState": 0
            },

            {

                "IdValue": "00000000-0000-0000-0000-000000000000",
                "IdExternalValue": "",
                "Value": "",
                "ItemState": 0
            }
        ]


    }],
    "Faults": [],
    "StatusCode": 0,
    "MergeResult": null
}

Below is the code snippet for the above.下面是上面的代码片段。

 RequestSpecification request = RestAssured.given();
request.header("Content-Type", "application/json")
 JSONObject requestParams = new JSONObject();
requestParams.put("data", Property.getProperty("data")); 
Response response = request.post(url);
JsonPath js = response.jsonPath();
JSONObject responseObject = new JSONObject(response.jsonPath().getJsonObject("WorkItems"));
 
 
Configuration configuration = Configuration.builder().jsonProvider(new JacksonJsonNodeJsonProvider()).mappingProvider(new JacksonMappingProvider()).build();
DocumentContext json = JsonPath.using(configuration).parse(jsonString);
String jsonPath = "WorkItems.CorrelationUId";
String newValue = "newCorrelationUId";
System.out.println(json.set(jsonPath, newValue).jsonString()); 

Added the below dependency with Rest assured.添加了以下依赖项,保证 Rest。 however, i am getting import conflict with jayway dependency "The import io.restassured.path.json.JsonPath collides with another import statement"但是,我遇到了与 jayway 依赖项的导入冲突“导入 io.restassured.path.json.JsonPath 与另一个导入语句冲突”

<dependency>
    <groupId>com.jayway.jsonpath</groupId>
    <artifactId>json-path</artifactId>
    <version>2.4.0</version>
</dependency>

below snippet helped me out updating the node values and selecting a part of the entire json using Jayway下面的代码片段帮助我更新了节点值并使用 Jayway 选择了整个 json 的一部分

  DocumentContext json = JsonPath.using(configuration).parse(file);
   String jsonPath  = "WorkItems[0].WorkItemAttributes[0].Value";
String newValue = "new title";
DocumentContext finaljson = json.set(jsonPath, newValue);
 DocumentContext context = JsonPath.parse(finaljson.jsonString());
HashMap<String, Object> requiredpartofJson= context.read("WorkItems[0]");

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用 REST assured 将 API 响应中的值存储为全局变量并将其作为 Cucumber 功能文件中的参数传递给另一个 API - How can i store a value as global variable from an API response and pass it to another API as parameter in Cucumber feature file using REST assured 如何将访问令牌值从一个 API 响应主体(在一个类中)提取到另一个 API 标头(在另一个类中)在 rest 保证代码中 - How to extract accesss token value from one API response body(in one class) into another API header(in another class) in rest assured code 两个REST API响应差异使用放心 - Two rest api response diff using rest assured 如何在json响应中获取数组的大小? 放心API、JAVA - How get size of an array in json response? rest assured API, JAVA 如何将JSON响应转换为Java列表-使用Rest Assure确保进行API测试 - How to convert JSON response to Java List- Using Rest Assured for API Testing 控制器,用于将jsp输入传递给Rest API,并使用spring MVC将响应传递给另一个jsp - controller for pass jsp input to Rest API and pass the response to another jsp using spring MVC 如何从get api获取csrf令牌并使用REST Assured将csrf令牌传递给另一个post api? - How to get csrf token from get api and pass csrf token to another post api using REST Assured? 使用Rest Assured从JSON响应获取所有ID - Getting all ids from a JSON response using Rest Assured 如何使用 Rest 将多个文件作为输入传递给 api - How to pass multiple files as a input to an api using Rest Assured 如何使用带有JSON响应的Rest Assured声明基于正文项? - How to assert based on body item with Rest Assured with JSON response?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM