简体   繁体   中英

How to reset/remove/clear the request body in rest-assured

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.

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;
        }

 }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM