cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
dpombal
Post Patron
Post Patron

HTTP POST request with form urlencoded data

Hi all,

 

I am trying to reproduce below API HTTP Post call , successful in Postman:

Postman configuration

  1. Authorization key with a tokensequence
  2. Body with 3 parameters and important select option x-www-form-urlencoded

 

HeaderHeaderParametersParameters

 

I tried several M codes with this template and modifications

 

let
auth_key ="XXXXXXXXXXXXX",
base_url = "www.WEBPAGE.com",
extension = "/api/datos_de_recarga/chargedata.php",
url = base_url &extension,
header= [#"Authorization" = auth_key,
#"Content-Type" = "application/x-www-form-urlencoded"],
content1= "{
""LOGIN"":""USER@domain.es"",
""PASSWORD"":""PASS"",
""INICIO"":""2019-11-01T00:00:00Z""
}",
webdata1 = Web.Contents(url, [Headers=header,Content = Text.ToBinary(content1)]),
response1 = Json.Document(webdata1)
in
response1

 

 

 

All with denied access

Regards

1 ACCEPTED SOLUTION

This option worked finally for me, will try to make code better using parameters, but is is a good start.

 

CODE OK

let
auth_key ="XXXXXXXXXXXXXXXXXXX",
base_url = "www.hostname.com",
extension = "/api/datos_de_recarga/chargedata.php",
url = base_url &extension,
header= [#"Authorization" = auth_key,
#"Content-Type" = "application/x-www-form-urlencoded"],
content1= "LOGIN=USER@DOMAIN.es&PASSWORD=XXXXXX&INICIO=2019-11-01T00:00:00Z",
webdata1 = Web.Contents(url, [Headers=[#"Authorization" = auth_key,
#"Content-Type" = "application/x-www-form-urlencoded"],Content = Text.ToBinary(content1)]),
response1 = Json.Document(webdata1)
in
response1

View solution in original post

5 REPLIES 5
artemus
Microsoft
Microsoft

Try using: Uri.BuildQueryString(record) to build your query string instead of manually entering it as a string. Might be some encoding issue.

can you assist me? i tried my characters with and without encoding...think this is not my current issue

where to use Uri.BuildQueryString(record)

First change

content1= "{
""LOGIN"":""USER@domain.es"",
""PASSWORD"":""PASS"",
""INICIO"":""2019-11-01T00:00:00Z""
}",

 

to

 

content1 =

[

   LOGIN = "USER@domain.es",

   PASSWORD = "PASS",

   INCIO = #datetimezone(2019, 11, 1, 0, 0, 0, 0, 0)

]

 

then change:

Text.ToBinary(content1)

with

Text.ToBinary(Uri.BuildQueryString(content1))

This option worked finally for me, will try to make code better using parameters, but is is a good start.

 

CODE OK

let
auth_key ="XXXXXXXXXXXXXXXXXXX",
base_url = "www.hostname.com",
extension = "/api/datos_de_recarga/chargedata.php",
url = base_url &extension,
header= [#"Authorization" = auth_key,
#"Content-Type" = "application/x-www-form-urlencoded"],
content1= "LOGIN=USER@DOMAIN.es&PASSWORD=XXXXXX&INICIO=2019-11-01T00:00:00Z",
webdata1 = Web.Contents(url, [Headers=[#"Authorization" = auth_key,
#"Content-Type" = "application/x-www-form-urlencoded"],Content = Text.ToBinary(content1)]),
response1 = Json.Document(webdata1)
in
response1

Wow, thanks so much for posting this...I spent some time in trying to get to this and your post had the final bit of info I needed. Many thanks 🙂

Helpful resources

Announcements
PBI Sept Update Carousel

Power BI September 2023 Update

Take a look at the September 2023 Power BI update to learn more.

Learn Live

Learn Live: Event Series

Join Microsoft Reactor and learn from developers.

Dashboard in a day with date

Exclusive opportunity for Women!

Join us for a free, hands-on Microsoft workshop led by women trainers for women where you will learn how to build a Dashboard in a Day!

MPPC 2023 PBI Carousel

Power Platform Conference-Power BI and Fabric Sessions

Join us Oct 1 - 6 in Las Vegas for the Microsoft Power Platform Conference.

Top Solution Authors
Top Kudoed Authors