Power Query,使用表单数据发出http POST请求

时间:2018-05-03 09:47:50

标签: excel powerquery

我有一个REST API,它只接受带有表单数据的POST请求。

我知道在Power Query中JSON请求是这样的:

let
    url = "https://example.com",
    body = "{ ""first_param"": ""AAAAA"",  ""second_param"": ""BBBBBB""}",    

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

如何发送表单数据?

2 个答案:

答案 0 :(得分:8)

使用Uri.BuildQueryString和Json.Document

let
    url = "https://example.com",
    body  = "{ ""first_param"": ""AAAAA"",  ""second_param"": ""BBBBBB""}",
    Parsed_JSON = Json.Document(body),
    BuildQueryString = Uri.BuildQueryString(Parsed_JSON),
    Source = Json.Document(Web.Contents(url,[Headers = [#"Content-Type"="application/json"], Content = Text.ToBinary(body) ] ))
in
    Source
顺便说一下,你最好直接将body构建成record,避免文本字符串和双引号的双倍)

答案 1 :(得分:1)

我以这种方式使用它,它的授权类型为基本,并编码了用户名和密码。

enter image description here

let
  url = "http://localhost:8091/_p/query/query/service?",

  body = "{
    ""statement"": ""SELECT ROUND((SUM(src.DUR) / COUNT(.)), 0) AS 'Mean Screen Time per day' FROM \r\n(SELECT
                  SUM(TONUMBER(source.DURATION)) AS DUR, source.startDate AS DATE FROM \r\n(SELECT startDate, DATE_DIFF_STR(completionDate,
                  startDate, 'second') AS DURATION, attributes.screen AS SCREEN \r\nFROM data WHERE type_ = \""Event\"" AND type is NOT MISSING and
                  startDate IS NOT MISSING and completionDate IS NOT MISSING \r\nand completionDate > startDate and attributes.screen IS NOT
                  MISSING) source GROUP BY source.startDate) src"",
                  ""pretty"":true,""timeout"":""600s"",""profile"":""timings"",""scan_consistency":"not bounded"",""client_context_id"":""xyz""}",

  Source = Json.Document(Web.Contents(
    url,[
        Timeout=#duration(0,0,120,0),
        Headers=[#"Authorization"="Basic YXB",#"Content-Type"="application/json"],
        Content=Text.ToBinary(body)
        ]
      )
    ),

  results = Source[results],

  #"Converted to Table" = Table.FromList(results, Splitter.SplitByNothing(), null, null, ExtraValues.Error),
  #"Expanded Column1" = Table.ExpandRecordColumn(#"Converted to Table", "Column1", {"MEAN _DURATION", "SCREEN"},{"MEAN DURATION","SCREEN"}),
  #"Changed Type" = Table.TransformColumnTypes(#"Expanded Column1",{{"MEAN_DURATION", type number}})

in

  #"Changed Type"