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

 
posted @ 2021-07-08 14:59  未来可期_Durant  阅读(285)  评论(0编辑  收藏  举报