1

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."

1 Answers1

0

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

Peter Thomas
  • 54,465
  • 21
  • 84
  • 248