POST期间,请求正文中的变量被{}替换 [英] Variable in request body replaced with {} during POST

查看:51
本文介绍了POST期间,请求正文中的变量被{}替换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用空手道DSL(0.6.1.1版),并且遇到以下情况:

I'm using Karate DSL (version 0.6.1.1), and I have the following scenario:

  Scenario:
    Given url baseURL
    * def email = "test_email@test_domain.com"
    * def query = "SELECT * FROM public.users where username='" + email + "' ORDER BY user_id ASC"
    * def dbUrl = databaseUrl + "databaseName"
    * def config = { username: '#(databaseUsername)', password: '#(databasePassword)', url: '#(dbUrl)', driverClassName: 'org.postgresql.Driver' }
    * def DbUtils = Java.type('utility.database.DbUtils')
    * def db = new DbUtils(config)
    * def results = db.readRow(query)
    * def supplierId = results.user_id
    * print "SUPPLIERID: " + supplierId

    Given path '/path/to/endpoint'
    And header content-type = 'application/json'
    And request { supplierId: #(supplierId) }
    When method POST
    Then status 200

utility.database.DbUtils 是一个自定义类,而 readRow()是该类的单行成员,该类返回代码> JdbcTemplate.queryForMap(query) .

utility.database.DbUtils is a custom class and readRow() is a single line member of that class that returns the result of JdbcTemplate.queryForMap(query).

运行此方案将提供以下日志:

Running this scenario gives the following log:


    10:53:17.477 [main] INFO  com.intuit.karate - [print] SUPPLIERID: 957750e7-ee6b-486d-977c-05c8ac7bb589
    10:53:17.506 [main] INFO  com.intuit.karate - request:{
      "supplierId": 957750e7-ee6b-486d-977c-05c8ac7bb589
    }
    10:53:18.372 [main] DEBUG com.intuit.karate - 
    1 > POST http://baseurl.com/path/to/endpoint
    1 > Accept-Encoding: gzip,deflate
    1 > Connection: Keep-Alive
    1 > Content-Length: 17
    1 > Content-Type: application/json
    1 > Host: base.url.com
    1 > User-Agent: Apache-HttpClient/4.5.3 (Java/1.8.0_162)
    {"supplierId":{}}

您可以看到,请求主体首先具有 supplierId 的值,即 957750e7-ee6b-486d-977c-05c8ac7bb589 .不过,一旦完成POST, supplierId 就是 {} .这似乎与 supplierId 由数据库调用填充有关.如果我用简单的 * def providerId ="957750e7-ee6b-486d-977c-05c8ac7bb589" 替换对数据库的调用,则请求正文将按预期显示.

You can see that at first the request body has the value of supplierId as 957750e7-ee6b-486d-977c-05c8ac7bb589. Once the POST is made though, supplierId is just {}. This seems to have something to do with the fact that supplierId is populated by a database call. If I replace the call to the database with a simple * def supplierId = "957750e7-ee6b-486d-977c-05c8ac7bb589", the request body appears as expected.

为什么在进行POST调用时用 {} 替换 supplierId ,如何阻止它执行此操作?

Why is supplierId replaced with {} when the POST call is made, and how do I stop it from doing that?

推荐答案

看起来像是类型转换问题.我通过将 * def providerId = results.user_id 替换为 * string providerId = results.user_id 来使其工作.

Looks like it was a type conversion issue. I got it working by replacing * def supplierId = results.user_id with * string supplierId = results.user_id.

这篇关于POST期间,请求正文中的变量被{}替换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆