通过API从云上的DB2获取SQL结果以进行Power Query

ekqde3dh  于 2022-11-07  发布在  DB2
关注(0)|答案(1)|浏览(129)

我尝试通过Excel Power Query连接到云上的db2。
根据文档,这是curl请求的格式:

curl -X POST   https://hostname.com/dbapi/v4/sql_query_export   -H 'authorization: Bearer MyToken'   -H 'content-type: text/csv'   -d '{"command":"select * from mytable"}'

我尝试通过GUI进行操作,但出现错误x1c 0d1x

我很肯定我做得不对,但我甚至不能谷歌如何传递我的参数。
有人能导航一下如何组装M代码吗?
我根据@nfgl回答尝试了此操作

let
    body  = [#"command"="select * from mytable"]
    ,json = Json.FromValue(body)
    ,wc = Web.Contents("https://hostname.com/dbapi/v4/sql_query_export", [Headers=[#"content-type"="text/csv", authorization="Bearer XXX"]])
    ,Source = Csv.Document(wc,[Delimiter=",", Encoding=65001, QuoteStyle=QuoteStyle.Csv])
in
    Source

然而不能匿名地到处去凭证ui:

当我尝试使用令牌的Web API时:

顺便说一句,所有的东西都可以用python实现:

import http.client

conn = http.client.HTTPSConnection("hostname.com")

payload = "{\"command\":\"select * from mytable\"}"

headers = {
    'content-type': "text/csv",
    'authorization': "Bearer XXX"
    }

conn.request("POST", "/dbapi/v4/sql_query_export", payload, headers)

res = conn.getresponse()
data = res.read()

print(data.decode("utf-8"))
e0uiprwp

e0uiprwp1#

您无法通过GUI执行此操作,命令JSON必须位于请求内容中,而content-type是您发送的内容,即JSON,请打开高级编辑器并执行以下操作

let
    url = "https://showcase.api.linx.twenty57.net/UnixTime/fromunixtimestamp",
    body  = [#"UnixTimeStamp"= 1589772280, #"Timezone"=""],
    json = Json.FromValue(body),
    wc = Web.Contents(url, [Headers=[#"Content-Type"="application/json"], Content=json]),
    Source = Csv.Document(wc,[Delimiter=",", Encoding=65001, QuoteStyle=QuoteStyle.Csv])
in
    Source

相关问题