RestAssured:无法确定如何序列化 content-type application/x-www-form-urlencoded。如何创建具有 key/value 结构的请求?
RestAssured : annot determine how to serialize content-type application/x-www-form-urlencoded. How to create request with key/value structure?
我有以下 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
框架创建相同的请求,
我尝试用 keys/value 创建一个地图并将其放入正文:
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
请求中包含在 body
中的 key/value
映射。选项卡 Params
为空。但是在代码中,在第二种情况下,body
部分中没有任何数据:
- 邮递员:
- 代码日志:
你快到了,
地图:
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);
当您使用 formParams()
时,请放心将 Content-Type
自动设置为 application/x-www-form-urlencoded
更新:
我猜您得到的是 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);
您需要将编码器配置导入为静态
import static io.restassured.config.EncoderConfig.encoderConfig;
检查此 以获取有关编码器配置的更多信息
如果仍有问题,请从 POSTMAN 执行以下步骤
在您的 Rest Assured 代码中的 given()
之后添加一个 .log().all()
并比较结果
我有以下 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
框架创建相同的请求,
我尝试用 keys/value 创建一个地图并将其放入正文:
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
请求中包含在 body
中的 key/value
映射。选项卡 Params
为空。但是在代码中,在第二种情况下,body
部分中没有任何数据:
- 邮递员:
- 代码日志:
你快到了,
地图:
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);
当您使用 formParams()
Content-Type
自动设置为 application/x-www-form-urlencoded
更新:
我猜您得到的是 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);
您需要将编码器配置导入为静态
import static io.restassured.config.EncoderConfig.encoderConfig;
检查此
如果仍有问题,请从 POSTMAN 执行以下步骤
在您的 Rest Assured 代码中的 given()
之后添加一个 .log().all()
并比较结果