URL encoding in DataWeave - mule

I need to hit a service which needs URL encoding in the query parameters and I have the input as below:
{
"test" : ["123", "124"]
}
which when I encode using https://www.urlencoder.io/ I get the below format:
%7B%0A%09%22test%22%20%3A%20%5B%22123%22%2C%20%22124%22%5D%0A%09%7D
and the above I need to pass in the query parameters.
I try to generate the above URL encoder output in mulesoft with the below dataweave:
%dw 2.0
output application/x-www-form-urlencoded
---
payload
but it gives me the below output, which is not what I want:
test=123&test=124
So please let me know how I can generate the below pattern in mule for the above input:
%7B%0A%09%22test%22%20%3A%20%5B%22123%22%2C%20%22124%22%5D%0A%09%7D

You are mixing two very different concepts.
application/x-www-form-urlencoded is a MIME Type that is typically used to POST a web form data over HTTP. I mentioned a web form, but technically it can be used send any "JSON like" data and when you do that, it becomes key=value pairs separated by & when there are multiple fields.For Example {"field1": "value1", "field2": "value2"} will become field1=value1&field2=value2 when represented as x-www-form-urlencoded
On the other hand, URL Encoding is used to "Percentage Encode" certain characters that are not allowed in URLs (like non ASCII characters) or that have special meaning for URLs (like ?, &) so that you can safely use it to construct a URL.
What you need is the encodeURIComponent function that you can use to encode your String. Also, URL encoding is for Strings not for JSON Objects. So you will need to write the JSON payload as String. Something like below
%dw 2.0
import encodeURIComponent from dw::core::URL
output application/java
---
encodeURIComponent(
write(payload, "application/json")
)
Keep in mind you will get different results depending on if you want to keep the indentation or not while writing the payload to String. For example this will give you a different (and much shorter) result then the one above.
%dw 2.0
import encodeURIComponent from dw::core::URL
output application/java
---
encodeURIComponent(
write(payload, "application/json", {indent: false}) // Shorter URL as it will not keep indentation
)
It is highly preferred not to keep indentations, if you are using this to generate a URL, as it will keep the URLs significantly shorter.

Related

Read values from Error ErrorType in dataweave

Need small help to retrieve values which are in error.errorType as per attached screenshot.
I need values of asString, identifier and namespace from error.errorType.
And need dataweave expression code to write in transform message in Mulesoft.
enter image description here
You could refer to these with the .(dot) notation and that should work for you?
Script
%dw 2.0
output application/json
---
{
errorAsString: error.errorType.asString,
errorIdentifier: error.errorType.identifier,
errorNamespace: error.errorType.namespace
}
Grabbed a screenshot for you from a sample app that i created to demonstrate this.

What type of format is this API response?

I am using an API that gives me response in this format :
{'captchaId': '11655487', 'code': '644SD843ADSH48678 (just some letters and numbers)'}
Looks like JSON but it uses different quotes.
Does anyone know what format is it??

How to get single parameter from JSON response in JMeter and use in other HTTP request?

I want to fetch one parameter from JSON response in the JMeter tool.
Currently, I am calling one API through JMeter and in response I got various parameter in jason format, but I want to fetch single parameter from that request and want to call another API Using that Parameter.
Use extractors to parse response and get any data from it. E.g. if your JSON response looks like this:
{
"TITLE": "Empire Burlesque",
"ARTIST": "Bob Dylan",
"COUNTRY": "USA",
"COMPANY": "Columbia",
"PRICE": "10.90",
"YEAR": "1985"
}
You can use this options:
JSON Path Extractor
JSON extractor
Regex extractor
SmartMeter's Boundary Body extractor - fastest solution, but you need SmartMeter
Of course you can use Beanshell, JSR22 or jQuery extractors.
After extracting data to variable my_title you can use this variable in another requests like this ${my_title}
Since JMeter 3.0 there is JSON Extractor which can execute arbitrary JSON Path queries against responses, this way you will be able to extract the data you require and store it into a JMeter Variable for later re-use.
See API Testing With JMeter and the JSON Extractor guide for comprehensive information and real-life use-case.

Input variables in Dataweave

I am reading through the Dataweave documentation and am stumped with the example below.
The documentation says
Input directives allow you to make any number of input sources
available in global variables, which can then be referenced in any
part of the Transform’s body. To reference one of these, you can just
call it by the name you defined in the directive.
Then follows up with the example below
%dw 1.0
%input in0
%output application/xml
---
payload
My questions:
In which scopes will mule look for the variable in0? Payload, Flow, Session or something else, and in which order?
In this example, where is in0 used? How does it help in this example?
Why would I need an input variable? Dataweave seems to allow flowVars.hello.
I think the example doesn't match the text. That example should be:
%dw 1.0
%input in0
%output application/xml
---
in0
By default the input (however you named it) will be the payload. You can just be explicit and optionally also define the payload as an input directive in the header, although this isn’t required.
%dw 1.0
%input payload application/xml
%output application/xml
---
{
a: payload
}
Further in the documentation there are better examples:
CSV Input Directive When defining an input of type CSV, there are a
few optional parameters you can add to the input directive to
customize how the data will be parsed.
header: boolean that defines if the first line in the data contains
headers
separatorChar: character that separates fields, ',' by default
quoteChar: character that defines quoted text, " " by default
escapeChar: character that escapes quotes, / by default
A CSV input directive with custom parameters set looks like this:
%input in0 application/csv header=true separatorChar=; When
header=true you can then access the fields within the input anywhere
by name. Ex: in0.userName.
When header=false you must access the fields by index, referencing
first the entry and then the field, Ex: in0[107][2]
Generally, you would not need to declare anything with %input directive. By default payload, flowVars, inbound/outbound properties, sessionVars etc are accessible in dataweave like they are in MEL.
MEL is same as Java, anything that you can write in java to debug (System.out) you can do it in MEL too.
%dw 1.0
%output application/java
---
{
GENDER: payload.gender,
test:flowVars.test,
testIp:inboundProperties.testIp,
testOp:outboundProperties.testOp
}
Look at this example for multiple inputs -
Multiple Inputs

Transform a Fixed width file in Mule

What is the best way to transform a fixed width file. Based on certain criteria i need to skip certain rows in the fixed width file. Should a custom java component be written for this or can some inbuilt transformers be used. I tried to search in the Mule docs but unable to find a suitable answer hence posting the question here.
If your file has common delimiters for the fields and supported file extension then data-mapper will be able to transform to csv, json etc. You may be able to use the other hard coded transformers.
For anything else you can extend the transform class.
Assuming you have following content in your file .
001MONSTARTWORK
001TUEPROGRESSWORK
001WEDTAKEABREAK
001THURESUMEWORK
001FRIFINISHWORK
001SATCHILLDOWN
001SUNGRABABEER
Below DW Script ll split the data and ll format it as JSON.
%dw 1.0
%output application/json
---
(payload splitBy "\n") map {
Id:$[0..2],
Day:$[3..5],
Shout:$[6..18]
}