Mr.D
Mr.D

Reputation: 7863

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??

Upvotes: 9

Views: 35396

Answers (3)

Viraj_Zanda
Viraj_Zanda

Reputation: 101

For the latest versions, this one works fine for POST method APIs as I tried to get the access token from Zoho APIs to pass for a 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

For APIs with no content use Content = "{}"

Reference - https://developers.de/2021/10/11/how-to-send-the-post-request-in-powe-query/

Upvotes: 1

Sergey Lossev
Sergey Lossev

Reputation: 1530

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 )

Upvotes: 16

KARTHIKEYAN.A
KARTHIKEYAN.A

Reputation: 20080

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"

Upvotes: 2

Related Questions