【问题标题】:Equivalent rest API call for: "heroku pg:credentials DATABASE_URL --app mypgapp"等效的 rest API 调用:“heroku pg:credentials DATABASE_URL --app mypgapp”
【发布时间】:2016-08-05 09:48:19
【问题描述】:
curl -n -X GET https://api.heroku.com/apps/mypgapp/addons/heroku-postgresql
-H "Content-Type: application/json"
-H "Accept: application/vnd.heroku+json; version=3"
返回带有相关 PG 附加信息的 JSON,但不提供 CLI 使用 pg:credentials 命令提供的连接字符串。我想通过 Heroku REST API 得到的是主机、端口、数据库名、用户和密码。
这可能吗?
【问题讨论】:
标签:
heroku
heroku-toolbelt
heroku-postgres
heroku-api
【解决方案1】:
我发现查看 Heroku CLI 的调试输出非常有用。在这种情况下,您可以从环境变量中获取查询字符串:
DEBUG=1 HEROKU_DEBUG=1 heroku config:get -a mypgapp DATABASE_URL ❖ ruby-2.3.0
heroku-cli/5.2.12-09f3ecc (darwin-amd64) go1.6.2 /Users/bjeanes/.local/share/heroku/cli/bin/heroku cmd: version
heroku-cli/5.2.12-09f3ecc (darwin-amd64) go1.6.2 /Users/bjeanes/.local/share/heroku/cli/bin/heroku cmd: commands
heroku-cli/5.2.12-09f3ecc (darwin-amd64) go1.6.2 /Users/bjeanes/.local/share/heroku/cli/bin/heroku cmd: config:get
--> GET /apps/mypgapp/config-vars
<-- 200 OK
<-- {"DATABASE_URL":"postgres://REDACTED@ec2-174-129-223-35.compute-1.amazonaws.com:5432/dfbe4e6vs4diqb"}
postgres://REDACTED@ec2-174-129-223-35.compute-1.amazonaws.com:5432/dfbe4e6vs4diqb
因此,您可以将其转换为 curl 请求并使用 jq 获取值:
$ curl -n -H 'Accept: application/vnd.heroku+json; version=3' \
https://api.heroku.com/apps/mypgapp/config-vars | jq .DATABASE_URL
postgres://REDACTED@ec2-174-129-223-35.compute-1.amazonaws.com:5432/dfbe4e6vs4diqb
这不是heroku pg:credentials 使用的API。它实际上使用了 Heroku 自己的 deprecated v2 API。