service responses binary data
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
service responses binary data
Hi,
I am trying to create a mock service that reads some binary data from a file. The mock service needs to send back a http response using just the binary data as the content. Since I do not know the encoding of the binary data, I cannot encode the binary to, e.g., hex string. I have tried to search for possible solutions, but I couldn't find what I am looking for. Can anyone please help me with this issue? Thank you very much in advance.
Regards,
Peter
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Hi,
Here is one example below of how to achieve this, I think you just need the last line to convert to hex string.
import java.io.FileInputStream
import java.io.File
File f = new File("/.rnd")
FileInputStream fis = new FileInputStream(f)
byte[] b = new byte[f.length()]
fis.read(b)
log.info javax.xml.bind.DatatypeConverter.printHexBinary(b) //returns a hex string
Regards,
Marcus
SmartBear Support
Did my reply answer your question? Give Kudos or Accept it as a Solution to help others. ⬇️⬇️⬇️
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
I've got same kind of problem.
I'm using Ready! API and 'the ServiceV for a rest service to mock a download of a file with an url.
The filename is the parameter of the REST api and the directory is set as a custom project property.
def fileDir = context.mockService.getProperty("fileDir").value def filename = requestContext.getProperty("filename") def file = new File(fileDir + '\\' + filename) mockResponse.contentType = 'application/octect-stream' def headers = mockResponse.getResponseHeaders() def headerKey = 'Content-Disposition' def headerValue = 'attachment; filename="' + filename + '"' headers[headerKey] = [headerValue] mockResponse.responseHeaders = headers if (file.exists() && file.isFile()){ byte[] fileBytes = file.getBytes() if (fileBytes.length > 0){ try{ //mockResponse.responseContent="bytes:" + fileBytes.length com.eviware.soapui.impl.rest.mock.RestMockResult mockResult = mockResponse.getMockResult() mockResult.setRawResponseData(fileBytes) //mockResponse.responseContent=javax.xml.bind.DatatypeConverter.printHexBinary(fileBytes) } catch(Exception e){ mockResponse.responseContent = "Exception:" + e.getMessage() + e } } else{ mockResponse.responseContent = "" } } else{ mockResponse.responseContent = "File does not exist!" }
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Binary is needed to support all kind of file types (.txt, .pdf, .docx, etc)
Also the code below doesn't work.
com.eviware.soapui.impl.rest.mock.RestMockResult mockResult = mockResponse.getMockResult() InputStream inputS = new ByteArrayInputStream(fileBytes); long length=fileBytes.length com.eviware.soapui.support.Tools.readAndWrite(inputS, length, mockResult.getOutputStream()); inputS.close();
When running, this exception is written to file:
java.io.IOException: Closed
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
I got it working:
import javax.servlet.http.HttpServletResponse import com.eviware.soapui.support.Tools import com.eviware.soapui.impl.rest.mock.RestMockResult def fileDir = context.mockService.getProperty("fileDir").value def projectPath = (new com.eviware.soapui.support.GroovyUtils(context)).projectPath fileDir = fileDir.replace('$projectDir', projectPath) fileDir = fileDir.replace("\\", "/") def filename = requestContext.getProperty("filename") def file = new File(fileDir + '\\' + filename) def response = mockRequest.httpResponse response.setContentType('application/octect-stream') def headerKey = 'Content-Disposition' def headerValue = 'attachment; filename="' + filename + '"' response.addHeader(headerKey, headerValue) if (file.exists() && file.isFile()){ byte[] fileBytes = file.getBytes() if (fileBytes.length > 0){ try{ InputStream inputS = file.newInputStream() long length=file.length() response.setContentLength( ( int )length ); Tools.readAndWrite(inputS, length, response.getOutputStream()); inputS.close(); return new com.eviware.soapui.impl.rest.mock.RestMockResult(mockRequest); } catch(Exception e){ mockResponse.responseContent = "Exception:" + e.getMessage() + e } } else{ mockResponse.responseContent = "" } } else{ mockResponse.responseContent = "File does not exist!" }
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Your example from post 5 helped me to sent back a non plain text file like PDF / ZIP / JPEG or whatever, Tools.readAndWrite was the key.
But due to my project usage after successfully sent back the response with
"return new com.eviware.soapui.impl.rest.mock.RestMockResult(mockRequest);"
my SoapUI has thrown the Exception "Failed to find MockResonse"
I had to change the call of Response.
Instead of return directly the content, I put the output stream response.getOutputStream()) into a requestContext variable:
Tools.readAndWrite(inputS, length, response.getOutputStream()); inputS.close(); requestContext.myResponse = response.getOutputStream(); return "MockResponse";
The "MockResponse" below the MockAction contains as Body only
${myResponse}
No error log entry anymore and everything works as expected.
