How do I read image/jpeg data from a SoapUI mock request?

Viewed 673

I am sending a POST request to my mock REST server in SoapUI. The content of the request is a jpeg image. The headers received by the mock server look like this:

Mon Mar 07 14:22:51 PST 2016:INFO:Headers: [Accept-Language:[en-us], 
Authorization:[Basic 6b9b0cd1-cd80-42e6-87ca-d9e7b9a12a1e], 
Host:[localhost:8080], 
Content-Length:[3253337], 
Content-Location:[myimage.jpg], 
Accept-Encoding:[gzip, deflate], 
User-Agent:[RestClient/1 CFNetwork/758.2.8 Darwin/15.3.0], 
Connection:[keep-alive], 
Accept:[application/json], 
Content-Type:[image/jpeg]]

When I try to save the content as a file using the request input stream, I get a zero length file. This is my code:

def name = mockRequest.getRequestHeaders().get("Content-Location")
def groovyUtils = new com.eviware.soapui.support.GroovyUtils(context)
def path = groovyUtils.getProjectPath() + "/photos/" + name[0]

def outputFile = new File(path)
if (!outputFile.exists()) {
    outputFile << mockRequest.request.inputStream
} else {
    log.error "attempted to overwrite existing file"
}

If I try to get the content as a string, the file has a lot more data, but it cannot be viewed as an image. This is the alternate version of the above:

    def requestBody = mockRequest.getRequestContent()
    outputFile << requestBody

When I try to view the file, the error message is that it can't be opened because it may be damaged or an unrecognized file format.

What is the correct way to save this data from the POST request as a jpeg file?

0 Answers
Related