httpie.action
httpie.action copied to clipboard
:zap: Human-friendly interactions with third-party web services through GitHub Actions
httpie.action
Human-friendly interactions with third-party web services through GitHub Actions :zap:
A general purpose HTTP client for GitHub Actions, wrapping the HTTPie CLI to enable human-friendly interactions with third-party web services that expose an API over HTTP in your development workflow.
Why not just use webhooks?
Great question! Webhooks will likely just work in the vast majority of cases :sparkles:
In certain situations though, this may be a better solution, as it provides more fine-grained control over the HTTP request that gets sent, for example you can:
- Form an entirely custom payload
- Use a variety of different authentication methods
- Use a variety of different request methods (
GET
,POST
,PATCH
, etc.) - Send a variety of different MIME types
- Follow any redirects
- Act on the HTTP response
- Basically, leverage all of the features of HTTPie :sparkles:
Super simple example
To POST
some JSON
data, {"hello": "world"}
, to https://httpbin.org/anything
on every push
to the repo:
workflow "Call external API" {
on = "push"
resolves = ["Call httpbin"]
}
action "Call httpbin" {
uses = "swinton/httpie.action@master"
args = ["POST", "httpbin.org/anything", "hello=world"]
}
More examples
Using output in a downstream action
In this more advanced, but somewhat contrived, example we'll open an issue in the current repository, and then comment and close that issue in subsequent actions.
Note, this is made possible since the response body is preserved in a file, $HOME/$GITHUB_ACTION.response.body
(along with response headers, in $HOME/$GITHUB_ACTION.response.headers
, and the entire response, in $HOME/$GITHUB_ACTION.response
). The response can then be parsed in downstream actions using jq
, a command-line JSON processor, which is pre-baked into the container.
action "Issue" {
uses = "swinton/httpie.action@master"
args = ["--auth-type=jwt", "--auth=$GITHUB_TOKEN", "POST", "api.github.com/repos/$GITHUB_REPOSITORY/issues", "title=Hello\\ world"]
secrets = ["GITHUB_TOKEN"]
}
action "Comment on issue" {
needs = ["Issue"]
uses = "swinton/httpie.action@master"
args = ["--auth-type=jwt", "--auth=$GITHUB_TOKEN", "POST", "`jq .comments_url /github/home/Issue.response.body --raw-output`", "body=Thanks\\ for\\ playing\\ :v:"]
secrets = ["GITHUB_TOKEN"]
}
action "Close issue" {
needs = ["Issue"]
uses = "swinton/httpie.action@master"
args = ["--auth-type=jwt", "--auth=$GITHUB_TOKEN", "PATCH", "`jq .url /github/home/Issue.response.body --raw-output`", "state=closed"]
secrets = ["GITHUB_TOKEN"]
}
Trigger another workflow
In this example, we'll trigger a separate workflow, via the repository's dispatches endpoint.
Note, $PAT
refers to a personal access token, created separately, and stored as a secret.
action "Trigger workflow" {
uses = "swinton/httpie.action@master"
args = ["--auth-type=jwt", "--auth=$PAT", "POST", "api.github.com/repos/$GITHUB_REPOSITORY/dispatches", "Accept:application/vnd.github.everest-preview+json", "event_type=demo"]
secrets = ["PAT"]
}
Presets
Some HTTPie presets are applied by default by the included config.json
, that make sense in the context of GitHub Actions, namely:
-
--check-status
: Causes a workflow to exit if the HTTP status is one of3xx
,4xx
, or5xx
-
--ignore-stdin
: Disables HTTPie's default behaviour of automatically reading STDIN, typically not desirable during non-interactive invocations -
--default-scheme=https
: Assumeshttps://
is the default prefix for URLs, so you can just say (for example)api.github.com/zen
as opposed tohttps://api.github.com/zen
-
--print=hb
: Prints both response headers and response body in the output
Plugins
A few authentication plugins are included:
-
httpie-api-auth
: Provides support for the ApiAuth authentication scheme -
httpie-aws-auth
: Provides support AWS authentication -
httpie-hmac-auth
: Provides support for HMAC authentication -
httpie-jwt-auth
: Provides support for JSON Web Tokens
More info
Usage docs for HTTPie are here.