-1

I am not using any POJO Classes. Instead i am using External json file as payload file for 2 of my API's (Get API and Delete API).

Add API >> Adds the book name, book shelf, other book details along with unique place_id. Delete API >> Used to delete a book from specific rack using the unique place_id from above.

Since I am using external json payload input file, please let me know the way to pass the "place_id" grabbed from GET API and send this place_id to DELETE API external json file and then use it

Add Place API below: This API returns unique Place_ID in the response

{
  "location": {
    "lat": -38.383494,
    "lng": 33.427362
  },
  "accuracy": 50,
  "name": "Frontline house 1",
  "phone_number": "(+91) 983 893 3937",
  "address": "2951, side layout, cohen 09",
  "types": [
    "shoe park",
    "shop"
  ],
  "website": "http://google.com",
  "language": "French-IN"
}

Delete Place API below:

{
  "place_id": "<need to insert place_id from above response>"
}
Guru Prasad
  • 13
  • 1
  • 5

1 Answers1

0

You can do this using json-simple library in java. json-simple maven repository

imports

import io.restassured.RestAssured;
import io.restassured.path.json.JsonPath;
import io.restassured.response.Response;
import org.json.simple.JSONObject
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;

import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

You have to get to the following;

  1. Retrieve place_id from Add Place API json response

  2. Read request body of Delete Place API from json file

  3. Replace the place_id value in request body of Delete Place API

  4. Write the new request body to Delete Place API json file

    // Retrieve place_id from Add Place API json response
    Response response = RestAssured
            .when()
            .get("add_place_api_url")
            .then()
            .extract()
            .response();
    
    JsonPath jsonPath = response.jsonPath();
    
    String placeId = jsonPath.get("place_id");
    
    // Read request body of Delete Place API from json file
    JSONObject jsonObject = new JSONObject();
    JSONParser parser = new JSONParser();
    try {
        Object obj = parser.parse(new FileReader("path/DeletePlaceRequestBody.json"));
    
        jsonObject = (JSONObject) obj;
    
        // Replace the place_id value in request body of Delete Place API
        jsonObject.replace("place_id", placeId);
    
    } catch (IOException e) {
        e.printStackTrace();
    } catch (ParseException e) {
        e.printStackTrace();
    }
    
    // Write the new request body to Delete Place API json file
    try {
        FileWriter file = new FileWriter("DeletePlaceRequestBody.json");
        file.write(jsonObject.toString());
    
        file.flush();
        file.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
    
kaweesha
  • 803
  • 6
  • 16