Scenario 5: REST service accepting HTTP POST requests in an HTML form - 6.1

Talend Components Reference Guide

EnrichVersion
6.1
EnrichProdName
Talend Big Data
Talend Big Data Platform
Talend Data Fabric
Talend Data Integration
Talend Data Management Platform
Talend Data Services Platform
Talend ESB
Talend MDM Platform
Talend Open Studio for Big Data
Talend Open Studio for Data Integration
Talend Open Studio for Data Quality
Talend Open Studio for ESB
Talend Open Studio for MDM
Talend Real-Time Big Data Platform
task
Data Governance
Data Quality and Preparation
Design and Development
EnrichPlatform
Talend Studio

This scenario describes how to upload data to a REST service using HTTP POST in a HTML form file. To do so, a Job is created to accept HTTP POST requests using the tRESTRequest component.

A predefined HTML form UploadFile.html is used to upload data to the REST service:

<form action="http://localhost:8088/UserInfo" method="post" enctype="multipart/form-data">
	<input type="file" name="id" />
	<button>upload</button>
</form>

The customer.xml that contains a user ID will be uploaded:

  <customer>
    <id>100</id>
  </customer>

Setting up the Job

Drop the following components from the Palette onto the design workspace: a tRESTRequest, a tJavaRow, a tXMLMap and a tRESTResponse.

Configuring the tRESTRequest component

  1. Double-click tRESTRequest in the design workspace to display its Basic settings view.

  2. In the REST Endpoint field, keep the default URI "http://localhost:8088/" where the REST-ful web service will be accessible for requests.

  3. Click the [+] button to add one line in the REST API Mapping table.

  4. Select the newly-added line and click the [...] button in the Output Flow column to add a schema for the output flow. Give the name for the schema UserInfo in this scenario.

    The schema editor opens. Click the [+] button to add a new line id of byte[] type and enter multipart in the Comment field. Click OK to save the schema.

  5. Back to tRESTRequest Basic settings view, select POST from the list in the HTTP Verb column.

  6. Fill the field in the URI Pattern column with "/UserInfo".

  7. Select Multipart in the Consumes list. Keep the default settings of the other options.

Configuring the tJavaRow component

  1. Connect tRESTRequest to tJavaRow using the Row > UserInfo connection.

  2. Double-click tJavaRow in the design workspace to display its Basic settings view.

  3. Enter the following code in the Code box to convert the message into Document type and print it in the console.

    String  result = new String(input_row.id,0,input_row.id.length);
    
    routines.system.Document doc = new routines.system.Document();
    
    doc.setDocument(org.dom4j.DocumentHelper.parseText(result));
    
    output_row.id = doc;
    
    System.out.println(result);

Configuring the tXMLMap component

  1. Connect tJavaRow to tXMLMap using the Row > Main connection.

  2. Double-click tXMLMap in the design workspace to open the Map Editor.

  3. Click the [+] button on the bottom left to add a column id of Document type for the input.

  4. Right-click the root node of the input table and select Rename in the contextual menu and rename it customer.

  5. Create a sub-element to the customer node by selecting Create Sub-Element in the contextual menu. Name the sub-element as id in the pop-up dialog box.

  6. Click the [+] button on the top right to add an output and name it as response.

  7. Click the [+] button on the bottom right to add a column body of Document type for the output.

  8. Right-click the root node of the input table and select Rename in the contextual menu and rename it customer.

  9. Create a sub-element to the customer node by selecting Create Sub-Element in the contextual menu. Name the sub-element as id in the pop-up dialog box.

  10. Select the id column from the input table and drop it onto the Expression field of the id node of the output table.

  11. Click OK to save the settings.

Configuring the tRESTResponse component

  1. Connect tXMLMap to a tRESTResponse using Row > response connection.

    The schema defined in tXMLMap is retrieved in tRESTResponse automatically.

  2. Select OK(200) from the Return status code list.

  3. Leave the rest of the settings as they are.

Saving and executing the Job

  1. Save the Job and press F6 to execute it.

  2. Run the UploadFile.html using a web browser.

  3. Click the Browse button to navigate to the customer.xml that contains the user ID and click upload.

  4. The HTTP POST request is accepted by the REST service and the user ID is sent back to the server.

    You can also view the response in the page source.

  5. The content of the input file is printed in the execution console.