Contact Us 1-800-596-4880

readUrl

readUrl(String, String, Object): Any

Reads a URL, including a classpath-based URL, and returns parsed content. This function works similar to the read function.

The classpath-based URL uses the classpath:` protocol prefix, for example, classpath://myfolder/myFile.txt where myFolder is located under src/main/resources in a Mule project. Other than the URL, readURL accepts the same arguments as read.

Parameters

Name Description

url

The URL string to read. It also accepts a classpath-based URL.

contentType

A supported format (or MIME type). Default: application/dw.

readerProperties

Optional: Sets reader configuration properties. For other formats and reader configuration properties, see Supported Data Formats.

Example

This example reads a JSON object from a URL. (For readability, the output values shown below are shortened with …​.)

Source

%dw 2.0
output application/json
---
readUrl("https://jsonplaceholder.typicode.com/posts/1", "application/json")

Output

{ "userId": 1, "id": 1, "title": "sunt aut ...", "body": "quia et ..." }

Example

This example reads a JSON object from a myJsonSnippet.json file located in the src/main/resources directory in Studio. (Sample JSON content for that file is shown in the Input section below.) After reading the file contents, the script transforms selected fields from JSON to CSV. Reading files in this way can be useful when trying out a DataWeave script on sample data, especially when the source data is large and your script is complex.

Source

%dw 2.0
var myJsonSnippet = readUrl("classpath://myJsonSnippet.json", "application/json")
output application/csv
---
(myJsonSnippet.results map(item) -> item.profile)

Input

{
  "results": [
    {
      "profile": {
        "firstName": "john",
        "lastName": "doe",
        "email": "johndoe@demo.com"
      },
      "data": {
        "interests": [
          {
            "language": "English",
            "tags": [
              "digital-strategy:Digital Strategy",
              "innovation:Innovation"
            ],
            "contenttypes": []
          }
        ]
      }
    },
    {
      "profile": {
      "firstName": "jane",
        "lastName": "doe",
        "email": "janedoe@demo.com"
      },
      "data": {
        "interests": [
          {
            "language": "English",
            "tags": [
              "tax-reform:Tax Reform",
              "retail-health:Retail Health"
            ],
            "contenttypes": [
              "News",
              "Analysis",
              "Case studies",
              "Press releases"
            ]
          }
        ]
      }
    }
  ]
}

Output

firstName,lastName,email
john,doe,johndoe@demo.com
jane,doe,janedoe@demo.com

Example

This example reads a CSV file from a URL, sets reader properties to indicate that there’s no header, and then transforms the data to JSON.

Source

%dw 2.0
output application/json
---
readUrl("https://mywebsite.com/data.csv", "application/csv", {"header" : false})

Input

Max,the Mule,MuleSoft

Output

[
 {
   "column_0": "Max",
   "column_1": "the Mule",
   "column_2": "MuleSoft"
 }
]

Example

This example reads a simple dwl file from the src/main/resources directory in Studio, then dynamically reads the value of the key name from it. (Sample content for the input file is shown in the Input section below.)

Source

%dw 2.0
output application/json
---
(readUrl("classpath://name.dwl", "application/dw")).firstName

Input

{
  "firstName" : "Somebody",
  "lastName" : "Special"
}

Output

"Somebody"