0
votes

I had set Content-Type in RequestSpecBuilder as "ContentType.JSON". But on making a GET request, I get Content-Type as "application/xml" in response. How do i get back a json response? I have tried below approaches: 1. Set content type in RequestSpecBuilder object using setContentType method of RequestSpecBuilder class to "ContentType.JSON" and pass RequestSpecBuilder object in spec method of RequestSpecification --- got "application/xml" in response

  1. Set content type in RequestSpecification object using contentType method of RequestSpecification and pass ContentType.JSON as parameter --- still got "application/xml" in response

Note: The webservice URL requires ".json" to be explicitly specified to get a json response else by default it returns a "xml" response. However, I wanted to set content type by using RequestSpecBuilder. Eg: for Json response: URL -- http://ergast.com/api/f1/2017/circuits.json for Xml response: URL -- http://ergast.com/api/f1/2017/circuits

Code:
@Test   
public void test_AddHeader() {      
        //Use of RequestSpecification
        String pathUrl = "http://ergast.com/api/f1/2017/circuits";
        RequestSpecBuilder requestSpecBuilder = new RequestSpecBuilder();
        requestSpecBuilder = requestSpecBuilder.        
                setBaseUri(pathUrl).
                setContentType(ContentType.JSON).
                addQueryParam("limit", "10"); //added query param       
        RequestSpecification addRequestSpec = requestSpecBuilder.build();       

        RequestSpecification httpRequest = RestAssured.given().spec(addRequestSpec).contentType(ContentType.JSON);      
        Response httpResponse = httpRequest.get();
        System.out.println(httpResponse.getContentType()); //returns application/xml
        System.out.println(httpResponse.getStatusLine());   //returns HTTP/1.1 200 OK
        System.out.println(httpResponse.getBody().asString());//returns XML response
    }
1

1 Answers

0
votes

You are expecting JSON from Response but you are passing setContentType to your RequestSpecBuilder. This will just create your POST payload in json format. It does not do anything to your response.

What you can do instead is Create a ResponseBuilder and do a setContentType to JSON there. Hope this will help you.