Forum Discussion

mmoser18's avatar
mmoser18
Frequent Contributor
4 years ago

How to return binary data from a mock?

In a mock service I want to return a REST-response from a binary file. I got as far as reading the file and I have the bytes in my hand now (for brevity I left away the code that comes up with path using a custom property):

...

log.info("reading data from '${path}'")
def file = new File(path)
log.info("read data: " + file.bytes)

 

But how do I fill these bytes into the mock's response? NOWHERE did I find any info on the how to set the response's content or the methods on a mockResponse! I recall that these used to be online somewhere but I only found dozens of pages that "are taking a REST".  😞

 

I tried with

return file.bytes

 

mockResponse = file.bytes


mockResponse.setContent(file.bytes)


mockResponse.setData(file.bytes)

 

...  but now I ran out of ideas. What's the magic incantation to assign the file's content to the mock service's response message body? It's annoying that seemingly so simple tasks can stop you in mid-flight (or should I say mid-flow?)...

2 Replies

    • mmoser18's avatar
      mmoser18
      Frequent Contributor

      OK - I changed the title off my thread to better mirror my actual issue.

       

      Your response *would* have answered my immediate question (how to put some data into the mock response payload), if that data were pure text.

       

      However, in our case that content needs to be in a special character set (UTF-16LE) and thus the embedding of ${content} into the response converts the data wrongly, as these are then obviously converted into UTF-8 during the send-process. But our client doesn't understand UTF-8, only said UTF16LE.

       

      So, my actual question is: given that I have the binary data (here an example response read from a file, but it could in principle be *any* binary data) in my hands - how do I get these exact bytes into the mock-response's payload unchanged? Is that at all possible with SOAP-UI?