Search code examples
excelpowerquerym

Power Query Expression error: A value of type "Record" cannot converted into type "Text"


I have the issue that I'm not able to execute the following code. The syntax seems to be okay, but when I try to execute it, I get the response, that:

Expression.Error: We cannot convert a value of type Record to type "Text". Details: Value=[Record] Type=[Type]

let
    body="{
    ""page"": ""1"",
    ""pageSize"": ""100"",
    ""requestParams"": {
        ""deviceUids"": [
            ""xxx-yyy-xxx-yyyy-xxxx"",
            ""yyy-xxx-yyy-xxxx-yyyy"",
            ""aaa-bbb-aaa-bbbb-aaaa"",
            ""ccc-ddd-ccc-dddd-cccc""
        ],
        ""entityColumns"": [
            {
                ""entityId"": ""144"",
                ""joinColumnName"": ""device_uid"",
                ""columnName"": ""device_random_date""
            }
        ],
        ""columnNames"": [
            ""ts"",
            ""device_uid"",
            ""1"",
            ""32"",
            ""55"",
            ""203"",
            ""204""
        ],
        ""startUnixTsMs"": ""1583413637000"",
        ""endUnixTsMs"": ""1583413640000"",
        ""columnFilters"": [
            {
                ""filterType"": ""eq"",
                ""columnName"": ""55"",
                ""value"": ""1234""
            }
        ],
        ""sortOrder"": [
            {
                ""column"": ""ts"",
                ""order"": ""DESC""
            },
            {
                ""column"": ""55"",
                ""order"": ""ASC""
            }
        ],
        ""entityFilters"": [
            {
                ""entityId"": ""144"",
                ""entityEntryIds"": [
                    ""12345-221-232-1231-123456""
                ]
            }
        ]
    }
    }",
    Parsed_JSON = Json.Document(body),
    BuildQueryString = Uri.BuildQueryString(Parsed_JSON),
    Quelle = Json.Document(Web.Contents("http://localhost:8101/device-data-reader-api/read-paginated/xxx-xxx-yyyy-yyyy", [Headers=[#"Content-Type"="application/json"], Content = Text.ToBinary(BuildQueryString)]))
in
    Quelle

I tried to remove the quotes of the numbers, but this leads to the same issue, as system complains it cannot convert numbers into text. I need the body which needs to be handed over with the request in order to do a POST request. What I'm doing wrong?


Solution

  • Since you seem to want to send this as application/json, I think you would change this bit in your code:

    Content = Text.ToBinary(BuildQueryString)
    

    to:

    Content = Text.ToBinary(body)
    

    and then you'd also get rid of the lines below (since you don't need them):

    Parsed_JSON = Json.Document(body),
    BuildQueryString = Uri.BuildQueryString(Parsed_JSON),
    

    I don't think you would need Uri.BuildQueryString unless you wanted to send as application/x-www-form-urlencoded (i.e. URL encoded key-value pairs).


    Unrelated: If it helps, you can build the structure in M and then use JSON.FromValue to turn the structure into bytes which can be put directly into the POST body. Untested example is below.

    let
        body = [
            page = "1",
            pageSize = "100",
            requestParams = [
                deviceUids = {
                    "xxx-yyy-xxx-yyyy-xxxx",
                    "yyy-xxx-yyy-xxxx-yyyy",
                    "aaa-bbb-aaa-bbbb-aaaa",
                    "ccc-ddd-ccc-dddd-cccc"
                },
                entityColumns = {
                    [
                        entityId = "144",
                        joinColumnName = "device_uid",
                        columnName = "device_random_date"
                    ]
                },
                columnNames = {
                    "ts",
                    "device_uid",
                    "1",
                    "32",
                    "55",
                    "203",
                    "204"
                },
                startUnixTsMs = "1583413637000",
                endUnixTsMs = "1583413640000",
                columnFilters = {
                    [
                        filterType = "eq",
                        columnName = "55",
                        value = "1234"
                    ]
                },
                sortOrder = {
                    [
                        column = "ts",
                        order = "DESC"
                    ],
                    [
                        column = "55",
                        order = "ASC"
                    ]
                },
                entityFilters = {
                    [
                        entityId = "144",
                        entityEntryIds = {
                            "12345-221-232-1231-123456"
                        }
                    ]
                }
            ]
        ],
        Quelle = Json.Document(
            Web.Contents(
                "http://localhost:8101/device-data-reader-api/read-paginated/xxx-xxx-yyyy-yyyy",
                [
                    Headers = [#"Content-Type" = "application/json"],
                    Content = Json.FromValue(body)
                ]
            )
        )
    in
        Quelle
    

    It might look a little weird (since M uses [] instead of {}, {} instead of [] and = instead of :), but just mentioning in case it helps.