Search code examples
javaapicucumberrest-assured

How to send String[] and String values on the same API's body using Rest Assured?


I'm trying to create a Test Automation for a POST API using Rest-Assured and Java.

This API has the following body as Application/JSON:

{
    "billing_address": {
        "region": "Sao Paulo",
        "region_id": 123,
        "country_id": "BR",
        "street": ["Av Paulista"],
        "postcode": "10577",
        "city": "SAO PAULO",
        "telephone": "555-555-1111",
        "company": "Test test",
        "firstname": "Fernando",
        "lastname": "Alves",
        "document": "0123444064"
     }
}

In my code, I developed the following method to send this values and the request:

public void criarPF (String srtAmbiente, String srtAPI, String srtToken, String srtRegion, String srtRegionID, String srtCountryID, String srtStreet,
                    String srtPostcode, String srtCity, String srtTelephone, String srtCompany, String srtFirstname, String srtLastname, String srtDocument) {
                    
                    String uriBase = srtAmbiente;
                    String Api = srtAPI;
                    
                    Map<String, String> addressContent = new HashMap<String,String>();
                    addressContent.put("region", srtRegion);
                    addressContent.put("region_id", srtRegionID);
                    addressContent.put("country_id", srtCountryID);
                    addressContent.put("street", srtStreet);
                    addressContent.put("postcode", srtPostcode);
                    addressContent.put("city", srtCity);
                    addressContent.put("telephone", srtTelephone);
                    addressContent.put("company", srtCompany);
                    addressContent.put("firstname", srtFirstname);
                    addressContent.put("lastname", srtLastname);
                    addressContent.put("document", srtDocument);
               
                
                    Map<String, Object> postContent = new HashMap<String,Object>();
                    postContent.put("billing_address", addressContent);

                    
                    request = RestAssured.given().contentType(ContentType.JSON)
                            .header("Authorization", "Bearer "+srtToken).with().body(postContent);

I received an error messenger that "street" isn't a String, it's a String[].
So, how can I send a String and String[] values in the same API's body?


Solution

  • You are setting street to a string value. Just change it like this:

    Map<String, Object> addressContent = new HashMap<>();
    addressContent.put("region", srtRegion);
    addressContent.put("region_id", srtRegionID);
    addressContent.put("country_id", srtCountryID);
    
    // Here wrap strStreet in a List
    addressContent.put("street", Arrays.asList(srtStreet));
    
    addressContent.put("postcode", srtPostcode);
    addressContent.put("city", srtCity);
    addressContent.put("telephone", srtTelephone);
    addressContent.put("company", srtCompany);
    addressContent.put("firstname", srtFirstname);
    addressContent.put("lastname", srtLastname);
    addressContent.put("document", srtDocument);