gpt4 book ai didi

java - RestAssured : annot determine how to serialize content-type application/x-www-form-urlencoded. 如何使用键/值结构创建请求?

转载 作者:行者123 更新时间:2023-12-04 08:19:20 26 4
gpt4 key购买 nike

我有以下 postman Collection :

{
"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<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: Cannot serialize because cannot determine how to serialize content-type 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>
如何使用 Rest Assured 正确构建此模式的请求?
  • 附加信息:

  • PostMan请求包含 key/value map 在 body .标签 Params是空的。但是在代码中,在第二种情况下,我在 body 中没有任何数据部分:
  • postman :

  • enter image description here
  • 代码日志:

  • enter image description here

    最佳答案

    你快到了,
    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);
    Rest Assured 设置 Content-Typeapplication/x-www-form-urlencoded当您使用 formParams() 时自动
    更新:
    我猜你会收到 Unsupported Media Type因为 Rest Assured 在 Content-Type 的请求中设置了默认字符集
    enter image description here
    试试下面的代码
    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 导入为静态
    import static io.restassured.config.EncoderConfig.encoderConfig;
    检查这个 link有关编码器配置的更多信息
    如果仍有问题,请从 POSTMAN 执行以下步骤
    enter image description here
    添加 .log().all()之后 given()在您的 Rest Assured 代码中并比较结果

    关于java - RestAssured : annot determine how to serialize content-type application/x-www-form-urlencoded. 如何使用键/值结构创建请求?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65565652/

    26 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com