When invoking a Web Service, you may have the requirement to retrieve, from its response binary, data, such as files. This can be performed quite easily with the Designer in a mapping.
This example shows how to retrieve a pdf sent by a WebService. It would be exactly the same procedure to retrieve an Excel file.
In this article, you will find the following steps :
- First step : reverse the http Rest metadata
- Second step : create a file metadata
- Third step : create your mapping
- Bonus step : how to make the file name dynamic ?
First step : reverse the http Rest metadata
You will find indications to help you to reverse the http Rest metadata here.
Once the nodes have been reversed, you'll have a BINARY node, which is automatically detected by the reverse wizard, in the response body.
Second step : create a file metadata
We need a metadata to save the binary content sent in the response, in a dedicated file. For that, you will create a File Server metadata and add a directory to store the files sent by the API :
In this directory, you need to add a new file :
Be sure that :
- Name is set.
- Physical Name is set, with the expected name of the pdf file.
- Type is set to DELIMITED or POSITIONAL, it doesn't matter, but needs to be set.
Then you need to add a property field to get the binary content of the file.
Call this property field exactly : 'binary_content' :
Third step : create your mapping
Remember that, to call the API, you need to map a rdbms source to the main node of the httpRest metadata, here the GET node.
In this example, we have chosen to map a field from a query, which selects one line.
For that, you can create a H2 metadata, with a H2 base in memory :
And create a query which returns one line :
Then, the binary content needs to be loaded through a stage, before being integrated into the file.
In this example, the stage is done on the H2 PUBLIC schema.
Be sure that the stage is configured like that :
Select the binary field and click on Enable Datatype
Set the datatype to BLOB :
And set the Execution Location to the source :
Finally, you can map the BINARY field to the binary_content property field of the file.
You're ready to execute your mapping !
Bonus step : how to make the file name dynamic ?
You need to add another property field and call it exactly 'file_path' :
Then this new field can be set with a dynamic value in the mapping :
Do not forget to add the extension '.pdf' at the end of the file_path !
You will find more information about flat file property fields here.