zerocode icon indicating copy to clipboard operation
zerocode copied to clipboard

Unable to pass array to body of request from previous API response.

Open vinayak5044 opened this issue 3 years ago • 6 comments

API response of previous POST request: "dtcIds" : [ "#37:1498", "#37:1499", "#37:1502", "#37:1503" ]

Need to pass this whole array to body of request - But After running getting in logs like: "request": { "headers": { "locale": "${locale}" }, "body": { "dtcIds": "${$.previousRequest.response.body.dtcIds[?(@.*)]}" }

In logs it seems like, zerocode_backSlash

I don't want backword slash there ....Please help me to do it. Please refer attached screenshot.

vinayak5044 avatar Jul 21 '22 07:07 vinayak5044

It should be pass to body(payload) like this only - "dtcIds" : [ "#37:1498", "#37:1499", "#37:1502", "#37:1503" ]

and not like - zerocode_backSlash1

vinayak5044 avatar Jul 21 '22 07:07 vinayak5044

@sparrowV Could you please look into it ?

vinayak5044 avatar Aug 01 '22 05:08 vinayak5044

Hi @vinayak5044 i have recently work on such type of issue in which i have created instance of JSONObject and passed the string(in your case "dtcIds") in the arguments and then with the help of JSONObject reference i have invoked jsonObject.optString("dtcIds").replace("", ""); , used sprinboot in my project.

ayushdubey755 avatar Aug 01 '22 11:08 ayushdubey755

Hi @ayushdubey755 Thanks for reply.

But In my case, Can you please help to understand how to create jsonObject because I am taking array "dtcIds" from previous api response and chained it in required API.

For more understanding please refer below attached screenshot: I want syntax to pass in red marked square which will reflect correct dtcIds list

zerocode_sample

vinayak5044 avatar Aug 02 '22 10:08 vinayak5044

//sample code @vinayak5044 private void initAuthor() { Path path = Paths.get(authorDumpLocation); try (Stream<String> lines = Files.lines(path)) { lines.forEach(line -> { //read and parse the line String jsonString = line.substring(line.indexOf("{")); try { JSONObject jsonObject = new JSONObject(jsonString);

                //construct the author object
                Author author = new Author();
                author.setName(jsonObject.optString("name"));
                author.setPersonalName(jsonObject.optString("personal_name"));
                author.setId(jsonObject.optString("key").replace("/authors/", ""));

This is the code that i was talking about i have also new to this concept and get to know about it during handson practice Here is the link to a video hope this will give you some help https://www.youtube.com/watch?v=24NrLl8EhDM&list=PLJ5_m4Y8xLkkxfo2Z1Lb0qjXnAZqIWCCZ&index=9

ayushdubey755 avatar Aug 02 '22 19:08 ayushdubey755

Hi @ayushdubey755

I tried but unable to do it.

I am confused where I need to write this method to replace regex and how to call it from json test cases file ?

vinayak5044 avatar Aug 08 '22 11:08 vinayak5044