JSON FILE Complex Request Payload - Notes By ShariqSP

Handling Complex Request Payload Using a File Object in RestAssured

In this section, we will discuss how to send complex JSON payloads using a File object in a POST request with RestAssured. This method is useful when you have a large JSON payload stored in a file and need to send it as the request body without manually constructing the JSON string.

Steps to Handle Complex Request Payload Using a File Object

  1. Create a File Object: Use the File class to point to the location of your JSON file. This file will contain the payload you want to send.
  2. Pass the File Object to RestAssured: Instead of passing the body content as a string or JSONObject, you can directly pass the File object to the .body() method in RestAssured.
  3. Send the POST Request: Use RestAssured's post() method to send the POST request, along with necessary headers and the body as the file.
  4. Assert the Response Status: Ensure the request was successful by asserting the response status code.

Example Code


            import io.restassured.RestAssured;
            import java.io.File;
            
            public class ApiTest {
            
                public static void main(String[] args) {
                    // Path to the JSON file
                    File jsonFile = new File("path/to/your/companyData.json");
            
                    // Send POST request with JSON file as the request body
                    RestAssured.given()
                            .header("Content-Type", "application/json")
                            .body(jsonFile)  // Pass the File object here
                            .post("http://apitesting.shariqsp.com:8080/api/companies")
                            .then()
                            .assertThat()
                            .statusCode(200);  // Assert that the response status code is 200
                }
            }
                

Explanation

  • File Object: The File jsonFile = new File("path/to/your/companyData.json"); creates a File object pointing to the location of the JSON file.
  • RestAssured Body: The .body(jsonFile) method tells RestAssured to send the contents of the file as the request body.
  • Content-Type Header: The header("Content-Type", "application/json") sets the request header to indicate that JSON is being sent.
  • Sending the Request: The post("http://apitesting.shariqsp.com:8080/api/companies") sends the POST request to the specified API endpoint.
  • Response Assertion: The .statusCode(200) checks if the response status code is 200, indicating the request was successful.

File Structure

The JSON file, companyData.json, should contain the structure of your payload, similar to the JSON you want to send. For example:


            {
                "name": "TechCorp",
                "location": "New York",
                "phone": "+1234567890",
                "email": "contact@techcorp.com",
                "lastUpdated": "2024-11-25T07:17:36.204Z",
                "mobileItems": [
                    {
                        "brand": "Samsung",
                        "model": "Galaxy S23 Ultra",
                        "price": 1199.99,
                        "currency": "USD",
                        "specifications": {
                            "processor": "Snapdragon 8 Gen 2",
                            "ram": "12GB",
                            "storage": "256GB",
                            "rearCamera": "200MP",
                            "frontCamera": "40MP",
                            "battery": 5000,
                            "os": "Android 13"
                        },
                        "warranty": {
                            "coverage": "Manufacturer",
                            "duration": 2
                        },
                        "availability": [
                            {
                                "store": "Best Buy",
                                "stock": 25
                            },
                            {
                                "store": "Amazon",
                                "stock": 50
                            }
                        ]
                    }
                ]
            }
                

Conclusion

Using a File object in RestAssured is a simple and effective way to handle complex request payloads stored in files. This method eliminates the need for manually constructing JSON objects in code and is particularly useful when dealing with large payloads. Simply point to the file and let RestAssured handle the rest of the process, ensuring seamless integration with your API.