cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
otravers
Continued Contributor
Continued Contributor

Forcing Web.Contents to make a GET call with a body

Hi,

 

I've used the following code pattern against several APIs to retrieve JSON content via Web.Contents, like so:

 

 

 

let
    getProjects = let
    relpath = "/api/path/here",
    Body = "
        {
            ""uid"": ""xyz"",
            ""api_key"": ""123"",
            ""secret_token"": ""456""
        }",
    Source = Json.Document(
        Web.Contents(
            "https://api.domain.com",
            [
                RelativePath = relpath,
                Content = Text.ToBinary(Body),
                Headers = [
                    #"Content-type" = "application/json"
                ]
            ]
        )
    ),
    response = try Source otherwise null
in
    Source
in
    getProjects

 

 

 

When I repurposed this code snippet to call a new (for me) API, I ran into a 404 Not found error. I triple checked my URL and relative path, all is correct. After running Advanced Diagnostics, I realized that all along this code pattern has been making POST requests, not GET. The reason I hadn't realized that in the past is that those specific APIs accept both GET and POST requests, but the new one doesn't.

 

Is there a way to force Web.Contents to stick to a GET HTTP request when you add a body (via Content) to the request? I need this body as this API uses it to pass along authentication parameters. Thanks for any help!

7 REPLIES 7
v-yingjl
Community Support
Community Support

Hi @otravers ,

As far as I know, Web.contents() sends GET request by default. But please note this optional parameter Content in it

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.

 

If you are using the Content parameter, it will change the web request from GET to POST compulsorily.

 

 

Best Regards,
Community Support Team _ Yingjie Li
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

otravers
Continued Contributor
Continued Contributor

Hi @v-yingjl thanks for confirming that using Content changes from GET to POST compulsorily. In my opinion this is undesirable behavior that prohibits from using some valid HTTP requests.

v-yingjl
Community Support
Community Support

Hi @otravers ,

Nevertheless, it is currently by design in power query. If you have any new ideas, you can create a new idea here:

https://ideas.powerbi.com/ideas/ 

 

Best Regards,
Community Support Team _ Yingjie Li
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

watkinnc
Super User
Super User

I wonder if Chris Webb's article will help. Seems to consider your issue.

https://www.google.com/amp/s/blog.crossjoin.co.uk/2014/03/26/working-with-web-services-in-power-quer... 


I’m usually answering from my phone, which means the results are visualized only in my mind. You’ll need to use my answer to know that it works—but it will work!!
otravers
Continued Contributor
Continued Contributor

@watkinncI fail to see how this article is addressing my specific issue, which again how to force Power Query to execute a GET HTTP request that includes a body.

My fault man. I know that the documentation for Web.Contents says that when a body is included, it becomes a POST request. I thought you were having trouble with the permissions/authentication with the API part of it, which the article talks about. Sorry my man!


I’m usually answering from my phone, which means the results are visualized only in my mind. You’ll need to use my answer to know that it works—but it will work!!
otravers
Continued Contributor
Continued Contributor

WebAction.Request with a get method sounds like it's the alternative to Web.Contents to address my requirement, but I cannot for the life of me find a single working example.

 

I'm getting "Expression.Error: We haven't been given permission to perform actions against this resource" like other people in various unresolved threads across the web.

 

Edit:  Chris Webb told me on Twitter that "those functions [WebAction.Request, WebMethod.Get/Post etc.] aren’t for public use and should really be hidden."

Helpful resources

Announcements
UG GA Amplification 768x460.png

Launching new user group features

Learn how to create your own user groups today!

November Power BI Update 768x460.png

Check it Out!

Click here to read more about the November 2021 Updates!

M365 768x460.jpg

Microsoft 365 Collaboration Conference | December 7–9, 2021

Join us, in-person, December 7–9 in Las Vegas, for the largest gathering of the Microsoft community in the world.

Top Kudoed Authors