Forum Discussion

otravers's avatar
otravers
Icon for Community Champion rankCommunity Champion
5 years ago

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!

9 Replies

  • otravers's avatar
    otravers
    Icon for Community Champion rankCommunity Champion

    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."

    • otravers's avatar
      otravers
      Icon for Community Champion rankCommunity Champion

      AnonymousI 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.

      • Anonymous's avatar
        Anonymous
        Not applicable

        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!

  • v-yingjl's avatar
    v-yingjl
    Icon for Community Support rankCommunity 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's avatar
      otravers
      Icon for Community Champion rankCommunity Champion

      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's avatar
        v-yingjl
        Icon for Community Support rankCommunity 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.