java.lang.IllegalStateException: You can either send form parameters OR body content in POST, not both!
一、问题现象
在使用rest-assured调用企业微信创建部门接口时,遇到如下问题:
java.lang.IllegalStateException: You can either send form parameters OR body content in POST, not both!
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
...
谷歌翻译是:
二、示例代码
package com.wechat; import io.qameta.allure.Description; import io.restassured.http.ContentType; import io.restassured.response.Response; import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; import static io.restassured.RestAssured.given; /** * @program: junit5Demo * @description: 企业微信部门增删改查 * @author: Durant.zeng * @create: 2021-07-08 10:25 **/ public class Demo_01 { static String accessToken; @BeforeAll static void getAccessToken() { accessToken = given ( ) .log ().all () .param ( "corpid", "wwc376242756245a88" ) .param ( "corpsecret", "oQ2Q1pRsobYehKX6xYCJ44g9qhIaC_s16tZvCBxGg9Q" ) .when ( ) .get ( "https://qyapi.weixin.qq.com/cgi-bin/gettoken" ) .then ( ) .log ( ).body ( ) .extract ( ) .response ( ) .path ( "access_token" ) ; } @Test @Description("创建部门") @DisplayName("创建部门") void createDepartment(){ String creatBody = "{\n" + " \"name\": \"广州研发中心\",\n" + " \"name_en\": \"RDGZ\",\n" + " \"parentid\": 1,\n" + " \"order\": 1,\n" + " \"id\": 2\n" + "}\n"; Response creatResponse = given() .contentType(ContentType.JSON) .param ( "access_token",accessToken ) .log ().all () .when() .body (creatBody) .post("https://qyapi.weixin.qq.com/cgi-bin/department/create" ) .then() .log().body() .extract() .response() ; } }
日志:
Request method: GET Request URI: https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=wwc376242756245a88&corpsecret=oQ2Q1pRsobYehKX6xYCJ44g9qhIaC_s16tZvCBxGg9Q Proxy: <none> Request params: corpid=wwc376242756245a88 corpsecret=oQ2Q1pRsobYehKX6xYCJ44g9qhIaC_s16tZvCBxGg9Q Query params: <none> Form params: <none> Path params: <none> Headers: Accept=*/* Cookies: <none> Multiparts: <none> Body: <none> { "errcode": 0, "errmsg": "ok", "access_token": "2pkjnCkj4XT3kzI4iX8pRz6noWnmyR6_JRXES5fc9felouUjnEU2RaXG8hU55ppMPK2kYzpKJvAaFfXPs7sowA6QQf9Rid1IreZ4V_8n7imm0uyIQf2sIle2DBnC32XLaPghsRvuYoWVnz8ue4ZPdYc5H43Qx72urkDBr2sCeHld6GcqJBuSHP9SqyCEfegfno2gNI7C_ygbhn0JLNNsBg", "expires_in": 7200 } Request method: POST Request URI: https://qyapi.weixin.qq.com/cgi-bin/department/create Proxy: <none> Request params: access_token=2pkjnCkj4XT3kzI4iX8pRz6noWnmyR6_JRXES5fc9felouUjnEU2RaXG8hU55ppMPK2kYzpKJvAaFfXPs7sowA6QQf9Rid1IreZ4V_8n7imm0uyIQf2sIle2DBnC32XLaPghsRvuYoWVnz8ue4ZPdYc5H43Qx72urkDBr2sCeHld6GcqJBuSHP9SqyCEfegfno2gNI7C_ygbhn0JLNNsBg Query params: <none> Form params: <none> Path params: <none> Headers: Accept=*/* Content-Type=application/json Cookies: <none> Multiparts: <none> Body: { "name": "广州研发中心", "name_en": "RDGZ", "parentid": 1, "order": 1, "id": 2 }
创建部门接口文档截图:
可见,access_token没有拼接到创建部门的URL后面
三、解决方案:
post请求的发送修改成下面样例
given(). queryParam("name, "value"). body(..). when(). post(..);
即param ()修改成queryParam()
日志:
Request method: GET Request URI: https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=wwc376242756245a88&corpsecret=oQ2Q1pRsobYehKX6xYCJ44g9qhIaC_s16tZvCBxGg9Q Proxy: <none> Request params: corpid=wwc376242756245a88 corpsecret=oQ2Q1pRsobYehKX6xYCJ44g9qhIaC_s16tZvCBxGg9Q Query params: <none> Form params: <none> Path params: <none> Headers: Accept=*/* Cookies: <none> Multiparts: <none> Body: <none> { "errcode": 0, "errmsg": "ok", "access_token": "2pkjnCkj4XT3kzI4iX8pRz6noWnmyR6_JRXES5fc9felouUjnEU2RaXG8hU55ppMPK2kYzpKJvAaFfXPs7sowA6QQf9Rid1IreZ4V_8n7imm0uyIQf2sIle2DBnC32XLaPghsRvuYoWVnz8ue4ZPdYc5H43Qx72urkDBr2sCeHld6GcqJBuSHP9SqyCEfegfno2gNI7C_ygbhn0JLNNsBg", "expires_in": 7200 } Request method: POST Request URI: https://qyapi.weixin.qq.com/cgi-bin/department/create?access_token=2pkjnCkj4XT3kzI4iX8pRz6noWnmyR6_JRXES5fc9felouUjnEU2RaXG8hU55ppMPK2kYzpKJvAaFfXPs7sowA6QQf9Rid1IreZ4V_8n7imm0uyIQf2sIle2DBnC32XLaPghsRvuYoWVnz8ue4ZPdYc5H43Qx72urkDBr2sCeHld6GcqJBuSHP9SqyCEfegfno2gNI7C_ygbhn0JLNNsBg Proxy: <none> Request params: <none> Query params: access_token=2pkjnCkj4XT3kzI4iX8pRz6noWnmyR6_JRXES5fc9felouUjnEU2RaXG8hU55ppMPK2kYzpKJvAaFfXPs7sowA6QQf9Rid1IreZ4V_8n7imm0uyIQf2sIle2DBnC32XLaPghsRvuYoWVnz8ue4ZPdYc5H43Qx72urkDBr2sCeHld6GcqJBuSHP9SqyCEfegfno2gNI7C_ygbhn0JLNNsBg Form params: <none> Path params: <none> Headers: Accept=*/* Content-Type=application/json Cookies: <none> Multiparts: <none> Body: { "name": "广州研发中心", "name_en": "RDGZ", "parentid": 1, "order": 1, "id": 2 } { "errcode": 0, "errmsg": "created", "id": 2 }
四、官网解释
Parameters
通常你指定这样的参数:
given(). param("param1", "value1"). param("param2", "value2"). when(). get("/something");
REST Assured 将根据 HTTP 方法自动尝试确定哪种参数类型(即查询或表单参数)。 在 GET 查询参数的情况下将自动使用,在 POST 表单参数的情况下将使用。 在某些情况下,将 PUT 或 POST 中的表单和查询参数分开是很重要的。 然后你可以这样做:
given(). formParam("formParamName", "value1"). queryParam("queryParamName", "value2"). when(). post("/something");
总结:在使用rest-assured发送请求时,使用get方法,则使用param;而使用post或put时,则使用queryParam或formParam
知道、想到、做到、得到
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· 上周热点回顾(2.24-3.2)