Categorygithub.com/visola/go-http-cli
module
0.9.2
Repository: https://github.com/visola/go-http-cli.git
Documentation: pkg.go.dev

# README

go-http-cli

Build Status Go Report Card

An HTTP client inspired by Curl, Postman and httpie made with :heart: in Go.

Getting Started

Download the latest release for your platform.

Unzip it and put all three binaries in you path adding something like this to your ~/.bash_profile:

export PATH=$PATH:/path/to/extracted/root

There's also an auto-completion helper for bash. You can add the following to your ~/.bash_profile:

complete -f -C go-http-completion http

What does it look like?

Example command pointing to a test server:

http \
  -H Content-Type=application/json \
  -X POST \
  -d '{ "name": "John Doe" }' \
  -V companyId=123456 \
  http://localhost:3000/api/v1/{companyId}/people

Output:

Usage

Usage of http:
  -c, --config keyValuePair     Path to configuration files to be used
  -d, --data string             Data to be sent as body
  -H, --header keyValuePair     Headers to include with your request
  -L, --location                Automatically follow redirects
      --max-redirs int          Maximum number of redirects to follow (default 50)
  -X, --method string           HTTP method to be used
  -V, --variable keyValuePair   Variables to be used on substitutions

Profiles

go-http-cli can use profile files which are just YAML files in a special location. The location by default points to ${user.home}/go-http-cli but it can be configured through the environment variable GO_HTTP_PROFILES.

IMPORTANT! Please don't store your passwords on plain text files! Use this only for local/development environments.

To activate a profile just add +profileName as part of your arguments. In this case, it would look for a ${user.home}/go-http-cli/profileName.{yml|yaml} file. It will fail if it can't find it.

What can I do with profiles? Many things, check it out for yourself:

Base URL

You can set a base URL for all your calls into that profile. A simple example:

baseURL:
  https://httpbin.org/

or

baseURL: https://httpbin.org/

Then calling /ip would automatically add the base URL like the following:

$ http +httpbin /ip

GET https://httpbin.org/ip
...

The path can be a relative path or an absolute path. The algorithm is very simple, it just concatenates baseURL with URL making sure only one / will exist between the two. You can also override a baseURL by passing a full URL from the command line (starting with http or https).

Headers

Setting up headers for all your requests is a pain. So you can put it in your profile like the following:

headers:
  Content-type:
    - application/json

or

headers:
  Content-type: application/json

And the headers will be added automatically:

http +httpbin /ip

GET https://httpbin.org/ip
Content-type: application/json
...

Variables

Variables can be added to the profile or passed from the command line to either override from the profile or add new values.

Variables will be replaced in the URL, body or headers. Just use them as {variableName} and it will be automatically replaced.

In your profile:

variables:
  companyId: 123456

Then you can make a call like the following:

$ http +httpbin /ip?companyId={companyId}

GET https://httpbin.org/ip?companyId=123456
Content-type: application/json
...

Requests

You can preconfigure requests inside a profile and then call them by name using @requestName. For example, if you had this in your profile:

requests:
  makePost:
    url: /post
    body: '{
      "username": "{username}",
      "companyId": {companyId}
    }'

You could make the following call:

$ http +httpbin @makePost -V username=test

POST https://httpbin.org/post
Content-type: application/json
>> { "username": "test", "companyId": 123456 }
...

Authentication

You can also configure authentication from your profile. Basic and bearer are supported. An example of basic authentication would look like the following:

auth:
  type: basic
  username: myUsername
  password: myPassword

Your username and password will be automatically encoded accordingly to RFC 7617 and set as a header for your requests:

$ http +httpbin /get

GET https://httpbin.org/get
Authorization: Basic bXlVc2VybmFtZTpteVBhc3N3b3Jk
Content-type: application/json
...

Bearer (also known as token) authentication is also supported:

auth:
  type: bearer
  token: myVerySecureToken
$ http +httpbin /get

GET https://httpbin.org/get
Authorization: Bearer myVerySecureToken
Content-type: application/json
...

Want to build from source?

This project uses Gradle as the build system. To run it, the only thing you'll need is to have Java installed. If you have a Go workspace correctly setup you can run the following to run a complete build.

cd $GOPATH
mkdir -p src/github.com/visola
cd src/github.com/visola
git clone https://github.com/visola/go-http-cli.git
cd go-http-cli
./gradlew updateDependencies updateLinter build

There are also tasks to build specific packages for the amd64 arch. You can build all the packages using:

./gradlew buildPackages

or for one specific platform:

./gradlew packageDarwinAMD64

# Packages

No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
Package request contains all the things used to create requests.
No description provided by the author
No description provided by the author