Forum Discussion
luckygirl
Helper I
1 year agoWeb.Contents Query parameter - multiple filed values for one parameter
Hi I am trying to specify multiple filed values for one parameter. I had a look at a similar post. https://community.fabric.microsoft.com/t5/Desktop/Web-Contents-Query-parameter-multiple-paramet...
- 1 year ago
The below code worked for me.
= let // List of subjects subjects = { "TEST1", "TEST2" }, // Function to fetch data for a single subject getDataForSubject = (subject as text) => let queryParams = [ #"per_page" = "100", #"and[display_collection][]" = "test", #"sort" = "date", #"direction" = "desc", #"and[subject][]" = subject, #"APIToken" = "123" ], source = Json.Document(Web.Contents("http://api.test.org/v1/records.json?", [Query = queryParams])) in source, subjectQuery = List.Transform(subjects, each getDataForSubject(_)) in subjectQuery
v-saisrao-msft
Community Support
1 year agoHi luckygirl,
Thank you for reaching out to the Microsoft Fabric Forum Community.
Thanks for the detailed post and for referencing Chris Webb’s blog—it’s a very helpful source when dealing with APIs that require repeated query parameters.
You're right in observing that, Web. Contents in Power Query don't allow duplicate field names in the Query record. So, while APIs often expect multiple values to be passed like this Power Query throws an error if you try to define "and[subject][]" more than once in the Query record.
let
baseUrl = "http://api.test.org/v3/records.json?",
subjects = {"Test1", "Test2"},
subjectQuery = Text.Combine(
List.Transform(subjects, each "and[subject][]=" & Uri.EscapeDataString(_)),
"&"
),
otherParams = "and[display_collection][]=Test&sort=date&direction=desc&APIToken=123",
fullUrl = baseUrl & subjectQuery & "&" & otherParams,
response = Json.Document(Web.Contents(fullUrl))
in
response
If this post helps, then please give us ‘Kudos’ and consider Accept it as a solution to help the other members find it more quickly.
Thank you.