【问题标题】:Post request body doesn't resolve variables发布请求正文不解析变量
【发布时间】:2021-08-16 18:21:23
【问题描述】:

我有一个简单的测试,使用如下所示的放心。 案例1:如果我传递json字符串如下,它工作正常。

@Test
    public void dummyTest throws Exception() {

       String newUserEmail = "postOrder" + UUID.randomUUID() + "@gmail.com";
       String json = "{\n" +
                 "    \"name\": \"Marisa\",\n" +
                "    \"birthday\": \"1997-10-06\",\n" +
                "    \"title\": \"Dr.\",\n" +
                "    \"email\": \"postOrder26@gmail.com\",\n" +
                "  },\n" 
       given()
                .baseUri("my_request_url")
                .header("Content-Type", "application/json")
                .body(json)
                .when()
                .post()
                .then()
                .log().all();
}

以上返回 200 响应。


但如果我编写测试如下,将电子邮件作为变量传递,返回 500

 @Test
    public void dummyTest throws Exception() {

       String newUserEmail = "postOrder" + UUID.randomUUID() + "@gmail.com";
       String json = "{\n" +
                 "    \"name\": \"Marisa\",\n" +
                "    \"birthday\": \"1997-10-06\",\n" +
                "    \"title\": \"Dr.\",\n" +
               "    \"email\": \""+newUserEmail+"\",\n" +
               "  },\n" 
       given()
                .baseUri("my_request_url")
                 .header("Content-Type", "application/json")
                .body(json)
                .when()
                .post()
                .then()
                .log().all();
}

我收到 500 错误响应

HTTP/1.1 500 Internal Server Error
Content-Type: text/html; charset=UTF-8
Transfer-Encoding: chunked
Connection: keep-alive
Date: Mon, 16 Aug 2021 17:05:36 GMT
Server: Apache
Cache-Control: no-cache, private
X-Cache: Error from cloudfront
Via: 1.1 871dedfc10f4428aa2412b6f788b791a.cloudfront.net (CloudFront)
X-Amz-Cf-Pop: ZRH50-C1
X-Amz-Cf-Id: xGkO8cJGV0Pq7wAx71YyglWzQNdZzooohqZLZBHkC54iZgiNhGmpEA==

<html>
  <head>
    <meta name="robots" content="noindex,nofollow"/>
    <style>
            /* Copyright (c) 2010, Yahoo! Inc. All rights reserved. Code licensed under the BSD License: https://yuilibrary.com/license/ */
            html{color:#000;background:#FFF;}body,div,dl,dt,dd,ul,ol,li,h1,h2,h3,h4,h5,h6,pre,code,form,fieldset,legend,input,textarea,p,blockquote,th,td{margin:0;padding:0;}table{border-collapse:collapse;border-spacing:0;}fieldset,img{border:0;}address,caption,cite,code,dfn,em,strong,th,var{font-style:normal;font-weight:normal;}li{list-style:none;}caption,th{text-align:left;}h1,h2,h3,h4,h5,h6{font-size:100%;font-weight:normal;}q:before,q:after{content:'';}abbr,acronym{border:0;font-variant:normal;}sup{vertical-align:text-top;}sub{vertical-align:text-bottom;}input,textarea,select{font-family:inherit;font-size:inherit;font-weight:inherit;}input,textarea,select{*font-size:100%;}legend{color:#000;}
            html { background: #eee; padding: 10px }
            img { border: 0; }
            #sf-resetcontent { width:970px; margin:0 auto; }
                            body { background-color: #fff; color: #222; font: 16px/1.5 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif; margin: 0; }
                .container { margin: 30px; max-width: 600px; }
                h1 { color: #dc3545; font-size: 24px; }
        </style>
  </head>
  <body>

谁能给我建议,如何解决这个 500 错误

【问题讨论】:

  • 对不起,您的代码示例充满了错误,所以我无法确定是哪一个导致的。请纠正他们。例如 public void dummyTest throws Exception --> 在方法名称后缺少 ()。语句末尾没有;
  • 服务器端是否记录错误?

标签: java rest-assured


【解决方案1】:

您的问题可能在这里得到解答: Grails - rendering UUID to JSON

UUID 数据类型不是字符串,而是位图数字,因此需要在连接之前转换为字符串。 .toString() 函数应该修复第二个块。我不确定 json 中“+”运算符的具体细节,但我相信类型转换与 C 一致。

【讨论】:

  • 字符串连接会自动调用toString
猜你喜欢
  • 1970-01-01
  • 2021-06-07
  • 2020-10-14
  • 2021-11-28
  • 2018-10-19
  • 2022-01-20
  • 1970-01-01
  • 2015-03-01
  • 2020-12-13
相关资源
最近更新 更多