接口和协议组成 Post 请求用 postman 构造时能正常响应,相同的参数用 rest assured 请求却提示参数格式异常,哪位大神能帮忙看下

彭红 for 新秀群 · 2018年06月09日 · 最后由 XS 回复于 2020年04月09日 · 3102 次阅读

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

共收到 13 条回复 时间 点赞

感觉问题出在 http header 上

postman 会加些 header 上去,可以用 fiddler 抓下,对比下 header。

Brook Shi 回复

对比了,postman 多有个 cookies,但我加上去后还是报异常,而且我和开发确认过那个 cookies 是不需要 的

没道理呀,可能只是个不起眼的 header 哦,比如 host。
实在不行只能让服务端开发打下 log 看看两次传上来的请求哪里不一样

你抓包看看,两次请求差异在哪里

于归 回复

看了 restassured 的请求日志,所有的参数和 postman 上一模一样

彭红 回复

😀 看请求报文有没有差别吧,或者把你的 restAssured 代码贴出来

对比下请求中的 Content-type,postman 的和 restassured 的,是否一致?

meiyo 回复

是一致的

彭红 #10 · 2018年06月11日 Author
于归 回复

贴出来了

小哥哥,可以用代码块调整一下格式么,exception 也贴一下。再帮你看。

彭红 #12 · 2018年06月16日 Author
卡农Lucas 回复

excetion 请看第二张图片

XS · #13 · 2020年04月09日
仅楼主可见
需要 登录 后方可回复, 如果你还没有账号请点击这里 注册