1.如下是用 postman 构造的 request,此 api 要同时传查询参数和 body.postman 构建请求返回为 200.

2.用 restassured 构造相同的请求,所有 参数完全一致,甚至参数名都是直接复制的,但总是提示参数格式异常。

个人怀疑问题出在传递的 body 中,因为另外一条只有 body 请求的 api 也 call 不通,传递的 body 和这条内容 是一样的。这个问题困扰我 2 天了😢

' public Response registerDevice(String strSWAccess2,String type,String serial,String deviceId,String sig,String ctr,String nounce) throws Exception {
givenWhetherNeedToLoginWithLID("yes");
JSONObject body=new JSONObject();
body.put("modelType",type);
body.put("sn",serial);

givenAnUrl(baseUrl+registerDevicePath);
//用 Lambda 表达式,用父类中带参数的方法发 Request,并拿回 Response
super.whenSendRequestWithMethod("post",x -> {

x.cookie("SWAccess", strSWAccess).
queryParam("deviceId", deviceId.trim()).
queryParam("ctr", ctr.trim()).
queryParam("nounce", nounce.trim()).
queryParam("sig", sig.trim()).
contentType("application/json").body(body).
header("Cache-Control","no-cache");

return x;
});
System.out.println("registerDevice return code:"+actualResponse.statusLine());
return actualResponse;
}
'
'
public void whenSendRequestWithMethod(String httpMethod, IFunc iFunc){

switch (HttpMethod.getHttpMethod(httpMethod)){
case GET:
actualResponse = iFunc==null?get((strURL)):iFunc.ISpecifyParams4HttpRequest(given()).get(strURL);
strResponse = actualResponse.asString();
break;

case POST:
//actualResponse = iFunc==null?given().contentType(JSON).body(strRequestBody).post(strURL):iFunc.ISpecifyParams4HttpRequest(given()).contentType(JSON).body(strRequestBody).post(strURL);
actualResponse = iFunc==null?given().log().all().post(strURL):iFunc.ISpecifyParams4HttpRequest(given().log().all()).post(strURL);
strResponse = actualResponse.asString();
break;

case PUT:
actualResponse = iFunc==null?put(strURL):iFunc.ISpecifyParams4HttpRequest(given().log().all()).put(strURL);
strResponse = actualResponse.asString();
break;

case DELETE:
actualResponse = iFunc==null?delete(strURL):iFunc.ISpecifyParams4HttpRequest(given().log().all()).delete(strURL);
strResponse = actualResponse.asString();
break;

default:
actualResponse =iFunc==null?get(strURL): iFunc.ISpecifyParams4HttpRequest(given().log().all()).get(strURL);
strResponse = actualResponse.asString();
break;
}

logger.info("Response = " + strResponse);
}
'


↙↙↙阅读原文可查看相关链接,并与作者交流