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

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
Anonymous
Not applicable

Building a Web API Custom Connector

Hi guys.

 

So I need to build a custom connector in order to allow a WebAPI connector to work in a gateway (I am currently under the impression that web connector cannot be used in a gateway with anonymous credentials - if this is wrong please let me know!)

 

I'm trying to recreate a query like this:

 

 

Source = Json.Document(Web.Contents( "URL for API",
[Headers = [ client_id = "XXX", client_secret = "XXX"],
RelativePath = 
"Dynamic URL 1",
& "Dynamic URL 2" & Parameter 1
& "Dynamic URL 3" & Parameter 2
])

 

 

Basically it supplies a baseURL to an API, with authentication headers passed through and then I have written a loop in M to enable a dynamic URL using parameters to cycle through all the variations I need. This is all good and works fine.

My problem, which I thought would be easy, is to basically create a web connector API. However, no matter how I shift around the code I can't quite get it to accept the headers and relativepath section properly. 

 

 

shared CustomConnector.Contents = 
(URL as text, 
 optional client_id as any , 
 optional client_secret as any, 
 optional RelativePath as record) =>
           let  
              headers =  [
                          client_id = client_id, 
                          client_secret = client_secret, 
                          RelativePath = RelativePath, 
                          #"Content-Type" = "application/json"],  
            
             jsonResults  = (Web.Contents(  
                                          URL , 
                                          [Headers = headers, ManualCredentials = true]))
                                                                
           in  jsonResults;

 

 

Any guidance on where I'm going wrong would be so appreciated!!!

1 ACCEPTED SOLUTION
PhilipTreacy
Super User
Super User

Hi @Anonymous 

You can rearrange the code like so 

 

 

shared CustomConnector.Contents = 
(URL as text, 
 optional client_id as any , 
 optional client_secret as any, 
 optional RelativePath as record) =>
 
           let  
              Options = [   Headers = [ client_id = client_id, 
                                        client_secret = client_secret, 
                                        #"Content-Type" = "application/json"
                                      ],  
              
                            RelativePath = RelativePath, 
                            
                            ManualCredentials = true 
                            
                        ], 
            
             jsonResults  = (Web.Contents( URL , Options))
                                                                
           in  jsonResults;

 

Regards

Phil


If I answered your question please mark my post as the solution.
If my answer helped solve your problem, give it a kudos by clicking on the Thumbs Up.



Did I answer your question? Then please mark my post as the solution.
If I helped you, click on the Thumbs Up to give Kudos.


Blog :: YouTube Channel :: Connect on Linkedin


Proud to be a Super User!


View solution in original post

3 REPLIES 3
PhilipTreacy
Super User
Super User

Hi @Anonymous 

You can rearrange the code like so 

 

 

shared CustomConnector.Contents = 
(URL as text, 
 optional client_id as any , 
 optional client_secret as any, 
 optional RelativePath as record) =>
 
           let  
              Options = [   Headers = [ client_id = client_id, 
                                        client_secret = client_secret, 
                                        #"Content-Type" = "application/json"
                                      ],  
              
                            RelativePath = RelativePath, 
                            
                            ManualCredentials = true 
                            
                        ], 
            
             jsonResults  = (Web.Contents( URL , Options))
                                                                
           in  jsonResults;

 

Regards

Phil


If I answered your question please mark my post as the solution.
If my answer helped solve your problem, give it a kudos by clicking on the Thumbs Up.



Did I answer your question? Then please mark my post as the solution.
If I helped you, click on the Thumbs Up to give Kudos.


Blog :: YouTube Channel :: Connect on Linkedin


Proud to be a Super User!


Anonymous
Not applicable

Thanks Philip that was super helpful. One issue is that with the RelativePath section (so called because I'm planning to wrap it in a RelativePath function to ensure it isn't appended to my baseURL when checking in the gateway) comes back with a "We cannot convert a value of type Record to type Text" - can I handle this problem in M, or is it an issue with my connector?

One additional question though, if I don't use the RelativePath function and provide simple text it works and in the datasource it produces, it successfully only uses the baseURL which is great but it inserts random forward slashes, so the data source ends up looking like:
"item\/thing\/join?curve\/" instead of "item/thing/join?curve"
I guess when adding to a gateway we could add the additional slashes into the URL, but do you know why it might be inserting these characters? Would prefer to remove them if possible?

If not, no worries and thank you so much for the help!

lbendlin
Super User
Super User

move the RelativePath section out of the headers section.

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

Find out what's new and trending in the Fabric Community.