Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Power Query, make http POST request with form data

I have a REST API, which accepts only POST requests with form data.

I know that in Power Query JSON requests are like this:

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

How it is possible to send form data??

like image 389
Mr.D Avatar asked May 03 '18 09:05

Mr.D


2 Answers

Use Uri.BuildQueryString and 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

btw, you'd better construct body directly into a record, avoiding text string and double of double quotes )

like image 71
Sergey Lossev Avatar answered Oct 19 '22 19:10

Sergey Lossev


I used this way it's working, Authorization type is Basic and encoded username and password.

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"
like image 2
KARTHIKEYAN.A Avatar answered Oct 19 '22 17:10

KARTHIKEYAN.A