This demonstrates how to upload an image or binary file to a URL using a stream in a QIE script.
// Create a new parameter map object using the 'qie' library. This would be used to add headers to the request.
var parameterMap = qie.newParameterMap();
// Headers are added with the "http.header." prefix like this example:
// parameterMap.put("http.header.headerName", "value");
//Since this example uses a stream vs. sent as a string, this header is not needed.
//parameterMap.put("http.header.encoding", "ISO-8859-1");
//Explicitly tell the server we are uploading binary data
parameterMap.put("http.header.content-transfer-encoding", "binary");
// Define the URL template for uploading a text file to a specific endpoint. This is used to override the endpoint in the web service connection.
var urlTemplate = qie.evaluateTemplate("http://localhost:8083/streamUploadImageFile");
// Open the image file as a stream. Change the file path to match your file's location.
var contentAsStream = java.io.FileInputStream("/Users/dir/Pictures/dog.jpeg");
var bufferedInputStream = new java.io.BufferedInputStream(contentAsStream);
// Call a REST web service using the provided parameters.
var result = qie.callRESTWebService(
"wsName",
urlTemplate,
"POST",
bufferedInputStream,
"image/jpeg",
parameterMap,
60000);
// Store the response from the web service call in a cache for further processing.
messageCache.setValue("restResult", result);
Let's break down this code step by step:
-
Setting Up Parameters:
- First, we start by creating a parameter map object using the 'qie' library. This object is used to hold parameters that we'll pass along with our request. In this example, it is used to specify headers for our HTTP request.
-
-
Content Transfer Encoding
- We set the
content-transfer-encoding
header to "binary". This tells the server that we're going to be sending binary data.
-
-
Defining URL Template:
- Next, we define the URL template. This is the endpoint where we want to upload our image file. In this example, the template points to "http://localhost:8083/streamUploadImageFile". Make sure to replace this with the actual URL of your endpoint.
-
-
Opening Image File as a Stream:
- Now, we open the image file as a stream. We use Java's
FileInputStream
to read the file. Note that you should replace the file path "/Users/dir/Pictures/dog.jpeg" with the actual path to your image file.
-
-
Calling the REST Web Service:
- We call a REST web service using the
callRESTWebService
function provided by the 'qie' library. We pass in several parameters:
'wsName'
: The name of the web service connection.
'urlTemplate'
: URL template of the endpoint used to override endpoint in web service connection.
'POST'
: The HTTP method we're using for the request, which is "POST" in this case since we're uploading data.
'bufferedInputStream'
: The stream containing our image data.
'image/jpeg'
: The content type of the data we're sending. This tells the server how to interpret our data.
'parameterMap'
: The parameter map object we created earlier, which may contain additional headers if needed.
'60000'
: Timeout value for the request in milliseconds (here it's set to 60 seconds).
-
-
Storing the Response:
- Finally, we store the response from the web service call in a cache named "restResult" for further processing. This allows us to access the response data later in the code.
-
That's it! This code demonstrates how to upload an image or binary file to a URL using a stream. Make sure to adapt it to your specific use case by modifying the URL, file path, and any other relevant parameters.
See also:
Handling HTTP requests and responses
Making Requests via qie.callRESTWebService()