How to read a file using readUrl function?

This blog demonstrates the use of readUrl function

How to read a file using read Url function.

  • readUrl() is part of core module. It takes three parameters as input& returns the content of the file.
  • readUrl(param1,param2,param3)
param1param2param3
urlcontentTypereaderProperties
The URL string to read. It also accepts a classpath-based URL.A supported format (or MIME type). Default: application/dw.Optional: Sets reader configuration properties.

Step 1:

Create a new mule application by clicking on File->New->Mule Project

Step 2:

Name the project appropriately and select the mule runtime as 4.3.0

Step 3:

The new project gets created successfully.

Step 4 :

Create a json file with name student.json in src/main/resources as shown below. Right click on src/main/resources. Go to New->File->student.json and paste the data given below in json file.

{

“Studentid” : “101”,

“Studentname” : “Sumit”

}

Step 5:

New file gets created as shown below. We are going to read this file using readUrl().

Step 6 :

Drag & drop http-listener & transform message component from Mule Palette in the Mule Configuration file.

Step 7 :

Configure http-listener with appropriate hostname ,port & path.

Step 8:

Add the Dataweave code to read the file as shown below:

Dataweave Code:

%dw2.0

outputapplication/json

readUrl(“classpath://student.json”,”application/json”)

Step 9 :

Save all the files. Right click on mule app->Run As->Mule Application

Step 10:

The mule app gets deployed and a success message is displayed in console as shown below:

Step 11: 

Go to postman. Create a new request & populate the following details:

HTTP Method: GET

Endpointhttp://localhost:8081/readfile

Step 12:

Click on Send button on the postman.

Step 13 :

We get a success response with http status code as 200 & response body as the content from the file.

Leave a Reply

Your email address will not be published. Required fields are marked *