In my karate tests i need to write response id's to txt files (or any other file format such as JSON), was wondering if it has any capability to do this, I haven't seen otherwise in the documentation. In the case of no, is there a simple JavaScript function to do so?
2 Answers
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:
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 theresponse
. 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 intarget/karate.log
) and also in the HTML report.see if
karate.write()
works for you as per this answerwrite 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.

- 54,465
- 21
- 84
- 248
My justification for writing to a file is a different one. I am using karate explicitly to implement a mock. I want to expose an endpoint wherein the upstream system will send some basic data through json payload using POST/PUT method and karate will construct the subsequent payload file and stores it the specific folder, and this newly created payload file will be exposed through another GET call.

- 127
- 1
- 7
-
I think you are still not "getting it" :) you can easily keep complex JSON-s in a data structure in-memory, so that POST requests make data available for future GET requests - see: https://github.com/intuit/karate/tree/master/karate-netty#the-worlds-smallest-microservice- – Peter Thomas Feb 14 '20 at 13:06