我必须将表单参数作为主体传递给我的请求。当我尝试时
Response post = request.urlEncodingEnabled(true).log().all().config(RestAssured.config()
.encoderConfig(EncoderConfig.encoderConfig()
.encodeContentTypeAs("x-www-form-urlencoded", ContentType.URLENC)))
我收到错误消息“您可以在 POST 中发送表单参数或正文内容,但不能同时发送!”
当我检查日志时,之前的 api 响应作为正文传递给了此请求。如何删除/重置/清除正文并仅传递表单参数。
最佳答案
您应该始终为每个请求使用新的 RequestSpecification 实例。
在每个新请求之前调用如下函数:
public void beforeNewRequest(){
restUtils.resetRestAssured(); //reset existing instance
restUtils = RestUtils.getInstance(); //get new instance
}
RestUtil.java 类
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;
}
}
关于java - 如何放心地重置/删除/清除请求正文,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58762017/