Forum Discussion
M language how to get this header to work?
Trying to create a custom connector but can't with this header for some reason. Not sure how to use it correctly.
This works just fine as a GET request
shared Connector.Contents = () =>
let
url = "URL",
apiKey = Extension.CurrentCredential()[Key],
headers = [
#"Authorization" = Text.Combine({"Bearer ", apiKey}),
#"Content-Type" = "application/json ; charset=utf-8",
#"Version" = "2021-08-16"
],
request = Web.Contents(url, [ Headers = headers, ManualCredentials = true ])
in
request;
But when having to preform a POST request to get data I'm having trouble with the added header called "POST"
shared Connector.DatabaseContents = () =>
let
url = "URL",
apiKey = Extension.CurrentCredential()[Key],
headers = [
#"POST" = "", //THIS IS THE LINE THAT I CANT GET TO WORK
#"Authorization" = Text.Combine({"Bearer ", apiKey}),
#"Content-Type" = "application/json ; charset=utf-8",
#"Version" = "2021-08-16"
],
request = Web.Contents(url, [ Headers = headers, ManualCredentials = true ])
in
request;
Using this in reqbin works fine using CURL, how do I translate this to M?
curl 'URL' \
-X 'POST' \
-H 'Authorization: Bearer APIKEY' \
-H 'Version: 2021-08-16' \
-H "Content-Type: application/json" \
Thanks in adance
That's not how POST works in Web.Contents()
Content: Specifying this value changes the web request from a GET to a POST, using the value of the option as the content of the POST.
9 Replies
- lbendlin
Super User
That's not how POST works in Web.Contents()
Content: Specifying this value changes the web request from a GET to a POST, using the value of the option as the content of the POST.
- JackSoderstrom
Helper I
Hey, Thank you for the reply. How do POST requests work with Web.Contents? Should I be formatting it like so?
let url = "URL", apiKey = Extension.CurrentCredential()[Key], headers = [ #"Authorization" = Text.Combine({"Bearer ", apiKey}), #"Content-Type" = "application/json ; charset=utf-8", #"Version" = "2021-08-16" ], request = Web.Contents(url, [ Headers = headers, ManualCredentials = true , Content = Text.ToBinary("POST")]) in request;I Get a 400 Bad Request with this
- lbendlin
Super User
instead of
Content = Text.ToBinary("POST")you need to specify the actual payload
Content = Text.ToBinary(payload)Here is an example of how to structure the payload string
M Query to use POST method on a Web API - Microsoft Power BI Community
- lbendlin
Super User
page_size 100 should do the trick.
- JackSoderstrom
Helper I
Still getting a 400 Bad request Error.
How should I format the dummy data?
let AuthKey = Extension.CurrentCredential()[Key], url = "https://api.notion.com/v1/databases/id/query", body = "{""page_size : ""100""}", //How should I be formatting this Source = Json.Document(Web.Contents(url,[ headers = [ #"Authorization" = Text.Combine({"Bearer ", AuthKey}), #"Content-Type" = "application/json ; charset=utf-8", #"Version" = "2021-08-16" ], Content = Text.ToBinary(body) ] )) in Source;- JackSoderstrom
Helper I
Figured it out, Thanks for all the help you definitely pointed me in the right direction. I ended up making it empty "Content = Text.ToBinary("")]" should have tried that first!