Identify query Params & uri Params in Mule
Soft?
Query
parameters and URI parameters are both used to pass data to a web service in
MuleSoft. However, there is a key difference between the two:
·
Query
parameters are passed in the
query string of the URL, after the question mark (?).
·
URI
parameters are passed in the
path of the URL, between curly braces ({}).
Here is an example of a URL with both query parameters and URI parameters:
https://example.com/api/products/{productId}?name=Alice
In
this example, the URI parameter is productId and the query parameter is name.
To
identify query parameters and URI parameters in MuleSoft, you can use the
DataWeave uriParams and queryParams functions.
The
uriParams function returns an object containing the URI parameters in the
current HTTP request. The queryParams function returns an object containing the
query parameters in the current HTTP request.
Code snippet
%dw 2.0
input = {
uri: "https://example.com/api/products/{productId}?name=Alice"
}
// Get the URI parameters.
var uriParams = input.uri | uriParams
// Get the query parameters.
var queryParams = input.uri | queryParams
// Log the URI parameters and query parameters.
log("URI parameters:", uriParams)
log("Query parameters:", queryParams)
Output:
URI parameters: {"productId": 1234}
Query parameters: {"name": "Alice"}
You can then use the uriParams and queryParams variables in your Mule flow to
access the data passed in the URI parameters and query parameters.
For example, you could use the uriParams. product
Id variable to access the product Id value. You could also use the
queryParams.name variable to access the name value.
No comments:
Post a Comment
Note: only a member of this blog may post a comment.