如何在Power Query公式语言中将请求对象作为查询字符串传递

时间:2016-07-14 22:59:06

标签: api rest powerbi powerquery

我正在尝试使用电源查询公式语言从power bi中的API中提取数据。

我的代码是:

let
obj= "{ ""dataset"": ""mydataset"",""queries"": [ { ""type"": ""single_measurement"", ""measure"": { ""aggregator"": ""unique_count"", ""column"": ""visitor_id"" } } ], ""start"":1451638800000,""end"":1468430640000,""max_groups"":1000,""group_by"":[""extrhike""]}",
authKey = "Token js+JG/FaGiZcFZPVAsAXmN+d20000",
url = "https:// myhostaddress.cloudapp.azure.com/api/v1/query?query=obj",

GetJson = Json.Document(Web.Contents(url,[Headers = [#"Authorization"=authKey, #"Content-Type"="application/json"]]))

in
GetJson

我想将数据请求对象(obj)作为查询字符串传递,如下所示

https://myhostaddress.cloudapp.azure.com/api/v1/query?query={ "dataset": "mydataset","queries": [ { "type": "single_measurement", "measure": { "aggregator": "unique_count", "column": "visitor_id" } } ], "start":1451638800000,"end":1468430640000,"max_groups":1000,"group_by":["extrhike"]}

我无法在电源查询中将obj作为查询字符串值传递。我收到以下错误

DataFormat.Error: Invalid URI: The hostname could not be parsed.
Details:
https:// myhostaddress.cloudapp.azure.com/api/v1/query?query=obj

如何在Power Query公式语言中将请求对象(obj)作为查询字符串传递。

提前致谢

4 个答案:

答案 0 :(得分:1)

您是否尝试使用Web.Contents的Query参数传递查询参数?

例如

let
    Url = "https:// myhostaddress.cloudapp.azure.com"
    Request = Json.Document(Web.Contents(url,
                                             [Headers = 
                                                      [#"Authorization"=authKey, 
                                                       #"Content-Type"="application/json"],
                                             RelativePath = "/api/v1/query",
                                             Query = [
                                                         <insert record here of your query object>
                                                     ]
                                             ]

查询记录的示例可能是:

Query = [#"Argument 1" = "Foo", #"Argument 2" = "Bar", limit = "1000", skip="500"]

您可以在此处阅读有关选项的信息:​​https://msdn.microsoft.com/en-us/library/mt260892.aspx

答案 1 :(得分:0)

您需要将字符串添加到一起。它应该是url = "https:// myhostaddress.cloudapp.azure.com/api/v1/query?query=" & obj,

答案 2 :(得分:0)

尝试使用此

它发布内容而不是GETting查询

let
    obj = "{ ""dataset"": ""mydataset"",""queries"": [ { ""type"": ""single_measurement"", ""measure"": { ""aggregator"": ""unique_count"", ""column"": ""visitor_id"" } } ], ""start"":1451638800000,""end"":1468430640000,""max_groups"":1000,""group_by"":[""extrhike""]}",
    authKey = "Token js+JG/FaGiZcFZPVAsAXmN+d20000",
    url = "https:// myhostaddress.cloudapp.azure.com/api/v1/query",

    GetJson = Json.Document(Web.Contents(url,[
        Headers = [#"Authorization"=authKey, #"Content-Type"="application/json"],
        Content = Text.ToBinary(obj)
    ]))
in
    GetJson

答案 3 :(得分:0)

您也可以使用此

它将obj转义为percent-string并附加到“query”参数

let
    obj= "{ ""dataset"": ""mydataset"",""queries"": [ { ""type"": ""single_measurement"", ""measure"": { ""aggregator"": ""unique_count"", ""column"": ""visitor_id"" } } ], ""start"":1451638800000,""end"":1468430640000,""max_groups"":1000,""group_by"":[""extrhike""]}",
    authKey = "Token js+JG/FaGiZcFZPVAsAXmN+d20000",
    url = "https:// myhostaddress.cloudapp.azure.com/api/v1/query?query="&Uri.EscapeDataString(obj),

    GetJson = Json.Document(Web.Contents(url,[Headers = [#"Authorization"=authKey, #"Content-Type"="application/json"]]))
in
    GetJson