【问题标题】:PowerBI Query WebMethod.Post returns Expression.Error: We cannot convert the value "POST" to type FunctionPowerBI Query WebMethod.Post 返回 Expression.Error:我们无法将值“POST”转换为函数类型
【发布时间】:2019-11-11 02:03:53
【问题描述】:

我使用的网站要求使用 Webform.Post 方法提交他们的 API 密钥和查询数据。我能够让它在 Python、C# 中工作,我什至能够构造和执行一个 cURL 命令,该命令返回一个 Excel 可以解析的可用 JSON 文件。我也在使用 Postman 来验证我的参数,并且使用所有这些方法看起来一切都很好。但是,我的目标是构建一个可以在 Excel 中使用但在 PowerBi Query 中无法通过此查询语法的查询表单。

现在我正在做一个简单的查询。该查询如下所示:

let
    url_1 = "https://api.[SomeWebSite].com/api/v1.0/search/keyword?apiKey=blah-blah-blah",

    Body_1 = {
            
            "SearchByKeywordRequest: 
            
            {
                ""keyword"": ""Hex Nuts"",
                ""records"": 0,
                ""startingRecord"": 0,
                ""searchOptions"": Null.Type,
                ""searchWithYourSignUpLanguage"": Null.Type
            }"
            
             },
    
    Source = WebMethod.Post(url_1,Body_1)
    
in
    Source

ScreenSnip showing valid syntax

它会产生以下错误:

Expression.Error: We cannot convert the value "POST" to type Function.
Details:
    Value=POST
    Type=[Type]

ScreenSnip of Error as it appears in PowerQuery Advanced Editor

在过去两天的大部分时间里,我都在尝试使用此方法或文档查找一些示例。 Microsoft 文档简单说明如下:

WebMethod.Post
04/15/2018
2 minutes to read

About
Specifies the POST method for HTTP.

https://docs.microsoft.com/en-us/powerquery-m/webmethod-post

这无济于事,到目前为止,我发现的唯一帖子批评海报没有使用 GET 与 POST。我会这样做,但我正在使用的网站支持。如果有人可以将我指向一个解释我做错了什么的文件或提出解决方案,我将不胜感激。

【问题讨论】:

    标签: powerbi powerquery webmethod


    【解决方案1】:

    WebMethod.Post 不是函数。它是一个常量文本值“POST”。您可以使用Web.ContentsWebAction.Request 函数发送POST 请求。

    一个发布 JSON 并接收 JSON 的简单示例:

    let
        url = "https://example.com/api/v1.0/some-resource-path",
        headers = [#"Content-Type" = "application/json"],
        body = Json.FromValue([Foo = 123]),
        source = Json.Document(Web.Contents(url, [Headers = headers, Content = body])),
        ...
    

    于 19 年 11 月 14 日添加

    请求正文必须是二进制类型,并包含在Web.Contents函数的第二个参数的Content字段中。

    您可以使用Json.FromValue 函数构造二进制 JSON 值。相反,您可以使用Json.Document 函数将二进制 JSON 值转换为对应的 M 类型。

    注意{}是M语言中的list类型,类似于JSON数组。 []record 类型,类似于 JSON 对象。

    话虽如此,您的查询应该是这样的,

    let
        url_1 = "https://api.[SomeWebSite].com/api/v1.0/search/keyword?apiKey=blah-blah-blah",
    
        Body_1 = Json.FromValue([
            SearchByKeywordRequest = [
                keyword = "Hex Nuts",
                records = 0,
                startingRecord = 0,
                searchOptions = null,
                searchWithYourSignUpLanguage = null
            ]
        ]),
    
        headers = [#"Content-Type" = "application/json"],
    
        source = Json.Document(Web.Contents(url_1, [Headers = headers, Content = Body_1])),
    
        ...
    
    参考:

    【讨论】:

    • 感谢您的回复。但是,我无法理解所述示例。所以让我换一种说法。我如何得到这个 Body_1 = { "SearchByKeywordRequest: { ""keyword"": ""Hex Nuts"", ""records"": 0, ""startingRecord"": 0, ""searchOptions"": Null.Type , ""searchWithYourSignUpLanguage"": Null.Type }" },
    猜你喜欢
    • 1970-01-01
    • 2022-12-22
    • 2023-04-03
    • 1970-01-01
    • 1970-01-01
    • 2022-07-31
    • 1970-01-01
    • 2020-11-23
    • 2021-06-01
    相关资源
    最近更新 更多