forked from remind101/empire
-
Notifications
You must be signed in to change notification settings - Fork 0
/
api.go
60 lines (52 loc) · 1.43 KB
/
api.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
package main
import (
"io"
"os"
"strings"
)
var cmdAPI = &Command{
Run: runAPI,
Usage: "api <method> <path>",
Category: "emp",
Short: "make a single API request" + extra,
Long: `
The api command is a convenient but low-level way to send requests
to the Heroku API. It sends an HTTP request to the Heroku API
using the given method on the given path. For methods PUT, PATCH,
and POST, it uses stdin unmodified as the request body. It prints
the response unmodified on stdout.
Method name input will be upcased, so both 'emp api GET /apps' and
'emp api get /apps' are valid commands.
As with any emp command, the behavior of emp api is controlled by
various environment variables. See 'emp help environ' for details.
Examples:
$ emp api GET /apps/myapp | jq .
{
"name": "myapp",
"id": "app123@heroku.com",
"created_at": "2011-11-11T04:17:13-00:00",
…
}
$ export HKHEADER
$ HKHEADER='
Content-Type: application/x-www-form-urlencoded
Accept: application/json
'
$ printf 'type=web&qty=2' | emp api POST /apps/myapp/ps/scale
2
`,
}
func runAPI(cmd *Command, args []string) {
if len(args) != 2 {
cmd.PrintUsage()
os.Exit(2)
}
method := strings.ToUpper(args[0])
var body io.Reader
if method == "PATCH" || method == "PUT" || method == "POST" {
body = os.Stdin
}
if err := client.APIReq(os.Stdout, method, args[1], body, nil); err != nil {
printFatal(err.Error())
}
}