在默认转换为字符串之前,无法读取CSV格式API响应

发布于 2025-02-01 18:15:30 字数 306 浏览 3 评论 0原文

方案:以JSON的要求和响应内容类型为文本/CSV 需要帮助 问题:当我试图将响应作为CSV文件获得时,无法获得该响应,因为空手道按照以下语句为字符串转换为字符串。

要求:

如何在转换为字符串之前将其读取为CSV响应文件? 如何停止作为字符串转换? “空手道将尝试将原始的HTTP响应主体解析为JSON或XML,并使其作为响应值可用。如果解析失败,空手道将记录警告,响应的值将是一个普通的字符串。您仍然可以执行字符串诸如匹配之类的比较包含并查找错误消息等。在极少数情况下,您可能需要检查响应的“类型”是什么,并且可以是3种不同的值之一:JSON,XML和String。”

Scenario: Post call with request as Json and response content type as text/csv
Need help
Issue: When I am trying to get the response as CSV file, unable to get it because Karate converts as String as per the below statement.

Requirement:

How to read as CSV response file before converting as String?
How to stop converting as String?
"Karate will attempt to parse the raw HTTP response body as JSON or XML and make it available as the response value. If parsing fails, Karate will log a warning and the value of response will then be a plain string. You can still perform string comparisons such as a match contains and look for error messages etc. In rare cases, you may want to check what the "type" of the response is and it can be one of 3 different values: json, xml and string."

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

×纯※雪 2025-02-08 18:15:30

我认为您需要花一些时间浏览文档: https://github.com/karatelabs /空手道#类型转换

因此,空手道可以将CSV格式的字符串转换为JSON,这就是您所需要的。

为了演示,只需尝试运行此测试即可。我找不到以CSV格式响应字符串的服务器,但是此行:* def Response = wendesp.data将为我们仿真。

Feature:

  Scenario:
    * url 'https://httpbin.org/post'
    * text body =
    """
    foo,bar
    1,2
    """
    * request body
    * method post
    
    * def response = response.data
    # you only need this one line to convert a csv response to json
    * csv response = response
    * match response == [{ foo: '1', bar: '2' }]

如果这没有意义,请假设空手道不支持您需要的东西 - 欢迎您寻找另一个框架。

请注意,一些团队编写自定义实用程序来支持自定义需求和边缘案例,并且永远不会“阻止”,您可以在此处找到更多信息: https://stackoverflow.com/a/47954946/143475

I think you need to spend some time going through the documentation: https://github.com/karatelabs/karate#type-conversion

So, Karate can convert a string in CSV format to JSON, and that's all you need.

To demonstrate, just try running this test. I couldn't find a server that responds with a string in CSV format, but this line: * def response = response.data will simulate that for us.

Feature:

  Scenario:
    * url 'https://httpbin.org/post'
    * text body =
    """
    foo,bar
    1,2
    """
    * request body
    * method post
    
    * def response = response.data
    # you only need this one line to convert a csv response to json
    * csv response = response
    * match response == [{ foo: '1', bar: '2' }]

If this doesn't make sense, please assume that Karate doesn't support what you need - and you are welcome to look for another framework.

Do note that some teams write custom utilities to support custom needs and edge cases, and are never "blocked", and you can find more information here: https://stackoverflow.com/a/47954946/143475

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文