Command line client for the API age HTTPie with OpenAPI
HTTPie is a helper for all API developers and users, which makes the better known cURL currently fierce competition. Especially in the OpenAPI area, the CLI client works very well.
The CLI client HTTPie provides some useful features for calling and debugging APIs, HTTP servers, and web services.
(Picture: HTTPie)
HTTPie describes itself as a” user-friendly command-line client for the API era ” – and thus hits the spot: Wherever you look, web services also offer open interfaces. Products can often be better and, above all, more individually used via the APIs – and own clients are relatively easy to implement.
For the use of web APIs and even more for testing, you often use cURL to formulate suitable HTTP requests. The syntax is long and sometimes quite cumbersome, answers are pale letter salad. For casual users, there have always been web services that specialize in such requests and offer a simple alternative to cURL, for example, ReqBin.
However, serious work is not possible in the long run and the open source tool HTTPie appears on the scene. The HTTP and HTTPS client provides simpler query syntax, supports JSON by default, provides output with syntax highlighting, can handle forms, and perform downloads as known from wget.
Perhaps the most exciting feature: HTTPie masters sessions, for example, in order not to have to pass authentications with every single request – the identification is then persistently maintained over several requests. HTTP-Prompt, which is based on HTTPie, puts another spin on it.
HTTP-Prompt offers syntax highlighting directly in the input, auto-completion and an OpenAPI/Swagger integration, with which endpoints can be searched simply like files in the normal Linux file system, simply by ls command. In addition, typing can be saved again, because users receive their own prompt according to the name – with the URL of the desired server instead of a local folder. So you can do without the URL part for inquiries.
HTTPie in use
The installation of HTTPie is trivial and takes place via the package manager of the Linux distribution used (“apt install httpie” or accordingly) or universally, also under Windows, by pip method:
python -m pip install --upgrade pip setuptools
python -m pip install --upgrade httpie
Then the commands “http” and “https” are available. For a first test, the following command is suitable:
http PUT pie.dev/put name=peter
With pie.dev exists a simple HTTP request and response service for testing. The result is a formatted, colorized output of response header and body. However, the output can also be extended or restricted, for example with:
http --print=HBhb PUT pie.dev/put name=peter
HTTPie output, limited to request header and body.
(Picture: Lang / HTTPie)
The header and body of the request (H and B) as well as the response (h and b) can be explicitly specified via” –print”. The options “– style” and “–pretty ” also allow a visually desired output, for example of responses in JSON format.
Real-world examples
How much easier life can be with HTTPie is shown by a real example in direct comparison with cURL: The following command provides all hosts of the monitoring solution Checkmk via its Swagger API:
curl -X GET
"http://192.168.178.68/mysite/check_mk/api/1.0/domain-types/host_config/collections/all"
-H "accept: application/json"
--header "Authorization: Bearer myusername mypassword" | python -m json.tool
Extract of cURL request.
(Picture: Lang / cURL)
The JSON response ultimately still needs to be formatted, which Python directly handles here. There are tools that provide prettier outputs, but so there are no more dependencies.
Now same request with HTTPie:
http
"http://192.168.178.68/mysite/check_mk/api/1.0/domain-types/host_config/collections/all"
'Authorization: Bearer myusername mypassword'
Extract of the http request.
(Picture: Lang / HTTPie)
It becomes clear: even with this simple request, there is already much less to type – and the result is also much more respectable. Usually, however, it does not stay with a single request; and in order not to have to pass things like the bearer token every time, named sessions are recommended – the first request:
http
session=meinesession
"http://192.168.178.68/mysite/check_mk/api/1.0/domain-types/host_config/collections/all"
'Authorization: Bearer myusername mypassword'
With the next request then suffices:
http
session=meinesession
"http://192.168.178.68/mysite/check_mk/api/1.0/domain-types/host_config/collections/all"
Of course, it is possible to use several sessions to play with different cookies, user roles and so on. But it is much shorter – with HTTP Prompt, which can be installed via pip:
pip install http-prompt
A simple call to a webhook now serves as an example:
curl -X POST 192.168.178.123:9000/hooks/webhook_test
Even with HTTPie alone could not save much here, with HTTP prompt, however, already:
http-prompt 192.168.178.27:9000
This means that you are logged in on the specified server and receive a corresponding prompt, on which you are allowed to work directly with HTTP methods such as POST:
http://192.168.178.27:9000> post hooks/webhook_test
For this POST request, HTTP Prompt internal is again used HTTPie. HTTP-Prompt also accepts options that are then automatically available to HTTPie, such as a session. How exactly HTTP-Prompt then shapes the http command is shown in a preview via the command ” httpie“:
http-prompt 192.168.178.27:9000 --session=meinesession
http://192.168.178.27:9000> httpie
Preview the http command in HTTP Prompt using “httpie”.
(Picture: Lang / HTTPie)
HTTPie then returns: “http — session=mysession http://192.168.178.27:9000” – this call is accepted if you only specify POST, GET or other methods directly on the prompt.
OpenAPI Access
We recently introduced Swagger or OpenAPI in more detail and HTTPie is the perfect companion for this. As an example, Checkmk should serve again, which offers a Swagger API and provides in the documentation in addition to cURL and Python code examples for HTTPie.
During the HTTP prompt call, three data are now passed with which HTTPie may subsequently work: Session, URL to the Swagger specification and authentication:
http-prompt 192.168.178.68
--spec=http://192.168.178.68/mysite/check_mk/api/1.0/openapi-swagger-ui.json
--session=foo
--username="myusername"
--password="mypassword"
What is new here is only the item “–spec”, via which the JSON file of the API is specified. This works just as well with a local JSON file, by the way. If you call the preview of the http command via httpie again, the result is:
http --session=foo http://192.168.178.68 --password=mypassword --username=myusername
To query then suffice again …
get mysite/check_mk/api/1.0/domain-types/host_config/collections/all
The autocomplete of HTTP Prompt.
(Picture: Lang / HTTPie)
The exciting question with APIs, which probably nobody knows inside and out, is: Which API endpoints are there? What “domain types” are there? In the hierarchy you can simply navigate with the well-known commands ls and cd – including autocompletion:
http://192.168.178.68> ls domain-types/host_config
… then shows the corresponding paths or endpoints-as known from navigation in the normal Linux terminal.
The two tools can do much more, for example pipes can be used to obtain content from somewhere via GET and to place it directly by POST elsewhere. Shell replacements can be used to read data from the normal shell into the HTTP Prompt environment (password = = ‘ echo ~/.mypassord’).
HTTPie can resume downloads or even pipe to tools like Tar while still displaying headers and progress – and the like. Anyone working with web APIs should definitely take a look at HTTPie and HTTP Prompt-those interested can also try the HTTPie client directly in the browser.
(ID: 47377556)