Is there a way to write to a text file using Karate

Try the karate.write(value, filename) API but we don’t encourage it. Also the file will be written only to the current “build” directory which will be target for Maven projects / stand-alone JAR.

value can be any data-type, and Karate will write the bytes (or plain-text) out. There is no built-in support for any other format.

Here is an example.

EDIT: for others coming across this answer in the future the right thing to do is:

  1. don’t write files in the first place, you never need to do this, and this question is typically asked by inexperienced folks who for some reason think that the only way to “save” a response before validation is to write it to a file. No, please don’t waste your time – and please just match against the response. You can save it (or parts of it) to variables while you make other HTTP requests. And do not write your tests so that scenarios (or features) depend on other scenarios, this is a very bad practice. Also note that by default, Karate will dump all HTTP requests and responses in the log file (typically in target/karate.log) and also in the HTML report.

  2. see if karate.write() works for you as per this answer

  3. write a custom Java (or JS function that uses the JVM) to do what you want using Java interop

Also note that you can use karate.toCsv() to convert JSON into CSV if needed.

Leave a Comment