0

I have to pass form-parameter as the body to my request. When I try

 Response post = request.urlEncodingEnabled(true).log().all().config(RestAssured.config()
            .encoderConfig(EncoderConfig.encoderConfig()
                    .encodeContentTypeAs("x-www-form-urlencoded", ContentType.URLENC)))

I am getting the error message as "You can either send form parameters OR body content in POST, not both!"

When I checked the log, previous api's response passed as body to this request. How to remove/reset/clear the body and pass only the form-parameter.

  • 1
    don't reuse requests. for each request start with a `given()`. See if this works `given()..urlEncodingEnabled(true).log().all().config(RestAssured.config().encoderConfig(EncoderConfig.encoderConfig().encodeContentTypeAs("x-www-form-urlencoded", ContentType.URLENC)))` – SudhirR Nov 19 '19 at 03:21
  • Worked after using given() for each request. – Murugesh Subramaniam Nov 19 '19 at 08:12

1 Answers1

1

You should always use a new RequestSpecification Instance for each request.

Before each new request call a function like:

public void beforeNewRequest(){
        restUtils.resetRestAssured();            //reset existing instance
        restUtils = RestUtils.getInstance();     //get new instance
    }

RestUtil.java class

public class RestUtils {

    private static RestUtils apiUtilsInstance = null;

        private RequestSpecification httpRequest;

        private RestUtils() {
            httpRequest = RestAssured.given();

        }

        public static RestUtils getInstance() {

            if (apiUtilsInstance == null)
                apiUtilsInstance = new RestUtils();

            return apiUtilsInstance;
        }

        public RequestSpecification getRequestSpecification() {
            return httpRequest;
        }

        public void resetRestAssured() {
            apiUtilsInstance = null;
        }

 }
Kushal Bhalaik
  • 3,349
  • 5
  • 23
  • 46