简体   繁体   English

如何放心地重置/删除/清除请求正文

[英]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!"我收到错误消息“您可以在 POST 中发送表单参数或正文内容,不能同时发送!”

When I checked the log, previous api's response passed as body to this request.当我检查日志时,以前的 api 响应作为正文传递给了这个请求。 How to remove/reset/clear the body and pass only the form-parameter.如何删除/重置/清除正文并仅传递表单参数。

You should always use a new RequestSpecification Instance for each request.您应该始终为每个请求使用一个新的RequestSpecification实例。

Before each new request call a function like:在每个新请求之前调用 function ,例如:

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

RestUtil.java class 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;
        }

 }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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