This tool is a simple command-line utility for sending HTTP requests to REST APIs. It supports common HTTP methods, custom headers, request bodies (from a flag or stdin), authentication (basic and bearer token), and even mTLS. You can also save your configuration to a YAML file for quick reuse.
- HTTP Methods: GET, POST, PUT, PATCH, DELETE
- Custom Headers: Specify multiple headers with
-H
- Request Body: Provide JSON (or any text) via the
-body
flag or via stdin - Authentication: Basic auth (
-username
and-password
) or Bearer token (-token
) - mTLS Support: Use client certificates with
-cert
,-key
, and-cacert
- Config Saving: Save your settings for reuse with the
-save
flag
- Clone the repository.
- Build the tool:
go build -o api main.go
Usage: api [flags] <url-path> [--]
<url-path>
: The endpoint path relative to the host or a full URL.--
: Optional separator to indicate that the request body should be read from stdin.
-
-host
The base URL or hostname (if scheme is omitted,https://
is assumed). -
-x
HTTP method to use (e.g. GET, POST, PUT, PATCH, DELETE). -
-username
and-password
Basic authentication credentials. -
-token
Bearer token for theAuthorization
header. -
-cacert
CA certificate file for mTLS. -
-cert
Client certificate file for mTLS. -
-key
Client key file for mTLS. -
-d
Request body. If it starts with{
or[
, the tool automatically setsContent-Type
toapplication/json
. -
-H
Custom header in the form"Key: Value"
(can be repeated). -
-save
Save all flag values to the config file (~/.api_config.yaml
) for reuse.
Below are examples using public REST APIs (all responses are in JSON).
Save your current settings to the configuration file for reuse:
./api -host https://httpbin.org -save /get
On subsequent runs, the saved values from ~/.api_config.yaml
will be used as defaults.
Send a GET request with a custom header:
./api -H "X-Custom-Header: MyValue" /get
Send a POST request with a JSON body using the -body
flag:
./api -x post -body '{"name": "John", "age": 30}' /post
Alternatively, read the request body from stdin:
echo '{"name": "John", "age": 30}' | ./api -method POST /post --
Access an endpoint that requires basic authentication:
./api -username user -password passwd /basic-auth/user/passwd
Send a request using a Bearer token:
./api -token your_token_here /bearer
If your API requires mTLS, provide your client certificate, key, and CA certificate:
./api -host https://your-secure-api.com -cert client.crt -key client.key -cacert ca.crt /your-endpoint
Note: Ensure you have valid certificate files for mTLS testing.
This API CLI Tool provides a flexible and lightweight way to test and interact with REST APIs directly from the command line. Customize your requests using a variety of options and streamline your workflow by saving your configuration. Feel free to contribute or open issues to help improve the tool!