RestAssured :不确定如何序列化内容类型应用程序/x-www-form-urlencoded如何使用键/值结构创建请求?

问题描述

我有以下 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",{
                            "key": "ClientIP","value": "200.55.111.111",{
                            "key": "RequestURL","value": "http://awesomeSite.com",{
                            "key": "ResourceType","value": "text/html",{
                            "key": "Method","value": "GET",{
                            "key": "Host","value": "awesomeSite.com",{
                            "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",{
                            "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",{
                            "key": "AcceptLanguage","value": "en-IL,en-US;q=0.9,en;q=0.8,my;q=0.7",{
                            "key": "AcceptEncoding","value": "gzip,deflate,br",{
                            "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:无法序列化,因为无法确定如何序列化内容类型应用程序/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 映射中的 body。标签 Params 为空。但是在代码中,在第二种情况下,我在 body 部分没有任何数据:

  • 邮递员:

enter image description here

  • 代码日志:

enter image description here

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)