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

oxosxuxt  于 2023-03-09  发布在  其他
关注(0)|答案(3)|浏览(352)

我有一个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

如何发送表单数据??

ahy6op9u

ahy6op9u1#

使用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(BuildQueryString) ] ))
in
    Source

顺便说一句,你最好直接把body构造成一个record,避免文本字符串和双引号)

46scxncf

46scxncf2#

我用这种方式工作,授权类型是基本和编码的用户名和密码.

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"
qlckcl4x

qlckcl4x3#

对于最新版本,这个方法适用于POST方法API,因为我试图从ZohoAPI获取访问令牌以传递给GET API。

let

SvcUrl = "https://****",

Content = "
{
 
  ""Tenant"": daenet"",
  ""filter"": {
    ""fields"": [
      {
        ""fieldName"": ""Field1"",
        ""operator"": 0,
        ""argument"": [
          ""003""
        ],
        ""distinct"": false
      }
    ]
  },
  ""fields"": [
    ""ProjectedField1"",""ProjectedField2"", ""EngTyp"", ""Name""
  ],
  ""top"": 100,
  ""skip"": 0,
  ""language"": ""001""
}
",

Response= Web.Contents(SvcUrl, 

[
Content=Text.ToBinary(Content),
Headers=[#"Content-Type" = "application/json"]
]
),

Json = Json.Document(Response)

in

  Json

对于没有内容的API,请使用Content = "{}"
参考-https://developers.de/2021/10/11/how-to-send-the-post-request-in-powe-query/

相关问题