Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Be one of the first to start using Fabric Databases. View on-demand sessions with database experts and the Microsoft product team to learn just how easy it is to get started. Watch now

Reply

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

1 ACCEPTED SOLUTION
lbendlin
Super User
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.

Web.Contents - PowerQuery M | Microsoft Docs

View solution in original post

9 REPLIES 9
lbendlin
Super User
Super User

page_size 100  should do the trick.

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;

 

 

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!

lbendlin
Super User
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.

Web.Contents - PowerQuery M | Microsoft Docs

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

 

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

Thank you for the reply, Is Content what you are wanting out of the request? If so when running this curl command 

curl -X POST 'URL' \
  -H 'Authorization: Bearer KEY' \
  -H 'Version: 2021-08-16' \
  -H "Content-Type: application/json" \

I get this

Capture.PNG

 Is there a way to grab the entire object to then go through it? or Can I just grab "results"? Ive tried to write the payload but haven't been able to get it to work. Thank you again

No, Content are the form fields (if you want) that you are adding to the request, not as URL parameters but as a payload in the body.

 

In your case you seem to want to run a POST without having an actual payload.  You could specify a fake payload or switch your request to GET.

Yeah, the POST request isn't sending any data it is just getting data but needs the "-X POST" to work. What type of Dummy payload would work? https://developers.notion.com/reference/post-database-query

This is the request if you are wanting more context. Im not sure why they don't just have this as a GET request but it is POST

Helpful resources

Announcements
Las Vegas 2025

Join us at the Microsoft Fabric Community Conference

March 31 - April 2, 2025, in Las Vegas, Nevada. Use code MSCUST for a $150 discount!

ArunFabCon

Microsoft Fabric Community Conference 2025

Arun Ulag shares exciting details about the Microsoft Fabric Conference 2025, which will be held in Las Vegas, NV.

December 2024

A Year in Review - December 2024

Find out what content was popular in the Fabric community during 2024.