restassured:无法确定如何序列化内容类型application/x-www-form-urlencoded如何使用键/值结构创建请求?

nfs0ujit  于 2021-06-27  发布在  Java
关注(0)|答案(1)|浏览(467)

我有以下几点 postman 收藏:

{
    "info": {
        "_postman_id": "b172f7d9-xxxxxxx",
        "name": "protection",
        "schema": "https://schema.getpostman.com/json/collection/v2.1.0/collection.json"
    },
    "item": [
        {
            "name": "protection-example_IP ONLY",
            "request": {
                "method": "POST",
                "header": [
                    {
                        "key": "Content-Type",
                        "value": "application/x-www-form-urlencoded"
                    }
                ],
                "body": {
                    "mode": "urlencoded",
                    "urlencoded": [
                        {
                            "key": "ApiKey",
                            "value": "62fdc812-be58-xxxxx",
                            "type": "text"
                        },
                        {
                            "key": "TagId",
                            "value": "1111",
                            "type": "text"
                        },
                        {
                            "key": "ClientIP",
                            "value": "200.55.111.111",
                            "type": "text"
                        },
                        {
                            "key": "RequestURL",
                            "value": "http://awesomeSite.com",
                            "type": "text"
                        },
                        {
                            "key": "ResourceType",
                            "value": "text/html",
                            "type": "text"
                        },
                        {
                            "key": "Method",
                            "value": "GET",
                            "type": "text"
                        },
                        {
                            "key": "Host",
                            "value": "awesomeSite.com",
                            "type": "text"
                        },
                        {
                            "key": "UserAgent",
                            "value": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537.36",
                            "type": "text"
                        },
                        {
                            "key": "Accept",
                            "value": "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9",
                            "type": "text"
                        },
                        {
                            "key": "AcceptLanguage",
                            "value": "en-IL,en-US;q=0.9,en;q=0.8,my;q=0.7",
                            "type": "text"
                        },
                        {
                            "key": "AcceptEncoding",
                            "value": "gzip, deflate, br",
                            "type": "text"
                        },
                        {
                            "key": "HeaderNames",
                            "value": "Host,User-Agent,Accept,Accept-Langauge,Accept-Encoding,Cookie",
                            "type": "text"
                        }
                    ]
                },
                "url": {
                    "raw": "https://other.awesome.com/test-api",
                    "protocol": "https",
                    "host": [
                        "other",
                        "awesome",
                        "com"
                    ],
                    "path": [
                        "test-api"
                    ]
                }
            },
            "response": []
        },
    //.........

我需要使用 rest-assured 框架,
我尝试创建一个带有键/值的Map,并将其放到主体中:

Map<String,String> keys = new HashMap<String, String>(){{
            put("ApiKey", "62fdc812-be58-xxxxxx");
            put("TagId", "1111");
            //...
 }};

RestAssured.given()
                .contentType("application/x-www-form-urlencoded")
                .body(keys)
                .when().post("https://other.awesome.com/test-api")
                .then()
                .statusCode(200);

但有个错误:
java.lang.illegalargumentexception:无法序列化,因为无法确定如何序列化内容类型application/x-www-form-urlencoded。
如果我使用 formParams 相反 body :

RestAssured.given()
                .log().all()
                .formParams(keys)
                .when().post("https://other.awesome.com/test-api")
                .then()
                .statusCode(200);

我得到以下错误:

Expected status code <200> doesn't match actual status code <415>
HTTP/1.1 415 Unsupported Media Type
Content-Length: 149
Content-Security-Policy: default-src 'none'
Content-Type: text/html; charset=utf-8
Date: Tue, 05 Jan 2021 09:50:12 GMT
X-Content-Type-Options: nosniff

<html lang="en">
  <head>
    <meta charset="utf-8"/>
    <title>Error</title>
  </head>
  <body>
    <pre>Unsupported Media Type</pre>
  </body>
</html>

如何正确构建这个模式的请求?
其他信息:
PostMan 请求包含 key/value Map中的 body . 标签 Params 是空的。但在代码中,在第二种情况下,我在 body 章节:
Postman :

代码日志:

kgsdhlau

kgsdhlau1#

你就快到了,
Map:

Map<String, String> keys = new HashMap<String, String>() {
            {
                put("ApiKey", "62fdc812-be58-xxxxxx");
                put("TagId", "1111");
            }
        };

放心代码:

RestAssured.given().formParams(keys).when().post("https://other.awesome.com/test-api").then()
                .statusCode(200);

放心好了 Content-Type 作为 application/x-www-form-urlencoded 当您使用 formParams() 更新:
我猜你会 Unsupported Media Type 因为rest assured在请求中设置了一个默认字符集 Content-Type
请尝试使用以下代码

RestAssured.given().log().all()
                .config(RestAssured.config()
                        .encoderConfig(encoderConfig().appendDefaultContentCharsetToContentTypeIfUndefined(false)))
                .formParams(keys).when().post("https://other.awesome.com/test-api").then().statusCode(200);

您需要将encoderconfig导入为static

import static io.restassured.config.EncoderConfig.encoderConfig;

有关编码器配置的更多信息,请查看此链接
如果您仍然有问题,从 Postman 遵循以下步骤

添加 .log().all() 之后 given() 在您放心的代码中,并比较结果

相关问题