2

I'm new to Karate

I'm automating an API test where I need to upload a large file >50MB When I do so with Karate I get an error "Broken Pipe" and according to this question Broken pipe (Write failed) when testing > max allowed Content-Length I could use "cURL" for this request.

It's working fine as follows (with hardcoded data):

* def result = karate.exec('curl -L -X POST "URL" -H "Authorization: Bearer MYTOKEN" -F "file=@"PATH""')

However, I'm having issues with the syntax when passing variables I need to pass the URL, token, and path as variables and not hardcoded text since I will be reusing this test for multiple landscapes.

How would I go about it? Thanks,

1 Answers1

1

Think of the Karate syntax as very close to JavaScript. So, string concatenation works. For example:

* def myUrl = 'https://httpbin.org/anything'
* def result = karate.exec('curl ' + myUrl)

And a nice thing is that JavaScript Template Literals work:

* def myUrl = 'https://httpbin.org/anything'
* def result = karate.exec(`curl ${myUrl}`)

Also note that the karate.exec() API takes an array of command-line arguments. This can make some things easier like not having to put quotes around arguments with white-space included etc.

* def myUrl = 'https://httpbin.org/anything'
* def result = karate.exec({ args: [ 'curl', myUrl ] })  

You can build the arguments array as a second step for convenience:

* def myUrl = 'https://httpbin.org/anything'
* def args = ['curl']
* args.push(myUrl)
* def result = karate.exec({ args: args }) 

Note that conditional logic and even an if statement is possible in Karate: https://stackoverflow.com/a/50350442/143475

Also see: https://stackoverflow.com/a/62911366/143475

Peter Thomas
  • 54,465
  • 21
  • 84
  • 248
  • Thank you! this worked. Another question, for this API I need to upload a zip file whose path is in the project. If I just read the file it passes the file as bytes instead of a zip. So I was able to get around it by doing this * def responseBytes = karate.read("file:classpath:com/PATH/file.zip) * def file = karate.write(responseBytes, 'temp-file.zip') * def result = karate.exec(`curl -L -X POST ${myUrl} \-H "Authorization: Bearer ${token}" \-F dta=@${file}`)

is there a more efficient/elegant way to do this? 
Thanks, – thecybergirl Aug 09 '22 at 21:28
  • @thecybergirl wow, I think you figured it out perfectly. what you can do is roll your own JS function, maybe call it in the `Background` and then make this a one-liner: https://github.com/karatelabs/karate#commonly-needed-utilities – Peter Thomas Aug 10 '22 at 04:27