1. 程式人生 > 其它 >java.lang.IllegalStateException: You can either send form parameters OR body content in POST, not both!

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

知道、想到、做到、得到