Ask a Question

how MOCK read request data from attachement


how MOCK read request data from attachement


exists some way, how to read this request in ReadyAPI MOCK service? 

My AUT (Application under test) sends text file in POST. I need to return JSON with somem value form the text file.

But how to do that?


My RAW request looks like:

POST http://localhost:9000/rest/submit HTTP/1.1
Content-Type: multipart/form-data; boundary="----=_Part_2_1159636768.1573464138308"
MIME-Version: 1.0
Content-Length: 132451
Host: localhost:9000
Connection: Keep-Alive
User-Agent: Apache-HttpClient/4.5.2 (Java/1.8.0_181)

Content-Type: application/octet-stream; name=example1.tdf
Content-Transfer-Encoding: binary
Content-Disposition: form-data; name="example1.tdf"; filename="example1.tdf"


I must extract this string "MBI20191107151008-IDE-00065" from it = each time will have differed value. And then send his value to the AUT in JSON.


How to read it? It is even possible in Soapui?

Champion Level 3



It is even possible in Soapui?

Yes, I believe that this should be possible in SoapUI.


But, considering that the question was posted to the ServiceVirtualization board but not to the SoapUI one, this is what I did using ServiceV:

-- For the POST /rest/submit VirtAction I set dispatch style to Script;

-- Pasted this code to the script dispatch window:

def attachments = mockRequest.getRequestAttachments();
// attachments.size();

//def attachmentName = attachments[0].name;
// "Name: $attachmentName";
// "URL: ${attachments[0].url}";
// "Part: ${attachments[0].part}";
// "Encoding: ${attachments[0].contentEncoding}";

def attachmentInputStream = attachments[0].inputStream;

def textValue = new String(attachmentInputStream.bytes, "UTF-8");
// textValue;
context.setProperty("textFileValue", textValue);
// This part of code will save attachment's content to the file
def attachmentContent = attachmentInputStream
def file = new File("D:\\$attachmentName");
// context.getProperty("textFileValue").toString();

-- For the response that will be sent as reply I opened Script section (will be on the right after you click on the response name) and entered this code:

import groovy.json.JsonSlurper
import groovy.json.JsonOutput

def textParam = context.getProperty("textFileValue").toString();
def payload = '{ "id" : "' + textParam + '"}';

def json = new JsonSlurper().parseText(payload.toString());;

mockResponse.responseContent = new JsonOutput().toJson(json);

And I got the value from request attachment as a value of the id json entry in response.

You will need to:

a) Write the code that will extract required value from the textFileValue property; and

b) Define the proper json structure for the payload variable.


Hope this will help.


P.S. In case you need to implement the same approach but in SoapUI, I believe that it will be not a problem to adopt the above code for SoapUI.


  /Alex [Community Champion]
[Community Champions] are not employed by SmartBear Software but
are just volunteers who have some experience with the tools by SmartBear Software
and a desire to help others. Posts made by [Community Champions]
may differ from the official policies of SmartBear Software and should be treated
as the own private opinion of their authors and under no circumstances as an
official answer from SmartBear Software.
The [Community Champion] signature is assigned on quarterly basis and is used with permission by SmartBear Software.
Showing results for 
Search instead for 
Did you mean: