Forum Discussion

JackSoderstrom's avatar
5 years ago
Solved

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

9 Replies

    • JackSoderstrom's avatar
      JackSoderstrom
      Icon for Helper I rankHelper 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

       

    • JackSoderstrom's avatar
      JackSoderstrom
      Icon for Helper I rankHelper 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's avatar
        JackSoderstrom
        Icon for Helper I rankHelper 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!