HTTPie - Command line HTTP Client

This guide provides brief overview on how to install HTTP command line client (CLI) named HTTPie and how to use it to send SPARQL queries to SPARQL endpoint.

Instroduction

HTTPie is name of an open source project providing HTTP command line interface. Actual binary is called http.

The CLI was designed to be easy to use - arguments, options and outputs are easy to learn, read and use.

Note, that there are also other HTTPie product such as Web or Desktop clients - feel free to use them, but this guide will not deal with them.

  • Product name: HTTPie

  • Client type: Command Line Interface (CLI)

  • Home page: https://httpie.io/cli

  • Binaries installed: http and https

  • Supported operating systems: MS Windows, Linux, MacOS, FreeBSD

  • Installers:

    • Universal: (python 3.7+) pip, pipx

    • MacOS: Homebrew, MacPorts

    • Windows: choco

    • Linux: Snapcraft, Linuxbrew, system package installers (apt, dnf, yum, pacman), binary executables

    • FreeBSD: FreshPorts,

Installation

For complete instructions see https://httpie.io/docs/cli/installation

Here are sample installation methods.

All specific platforms mentioned below also support Universal method, which is described as the last one.

On MS Windows

Using Chocolatey:

$ choco install httpie

On Linux

Using Snapcraft:

$ sudo snap install httpie

Using apt (Debian/Ubuntu etc.):

$ sudo apt install httpie

On MacOS

Using Homebrew:

$ brew update
$ brew install httpie

Using MacPorts:

$ port selfupdate
$ port isntall httpie

Universal (python) method

This method shall work on any platform supporting Python v3.7+

Using pipx (https://pypa.github.io/pipx/):

$ pipx install httpie

Using pip from python, installing into user profile:

$ python -m pip install --user pip wheel
$ python -m pip install --user httpie

Usage

For coplete help:

$ http --help

Basics

The call has synopsis:

$ http [flags] [METHOD] URL [ITEM [ITEM]]

When using https binary, there is no need to specify https:// part of the url, so following calls are identical:

$ http https://lod.tamtamresearch.com/sparql
$ https lod.tamtamresearch.com/sparql

Default method is GET so following methods are identical:

$ https GET lod.tamtamresearch.com/sparql
$ https lod.tamtamresearch.com/sparql

Query parameters

With HTTPie, query parameters do not have to be put into url and can be specified as extra ITEM arguments at the end using == delimiter between parameeter name and value.

Following calls are identical:

$ https httpbin.org/get?a=alfa&b=beta
$ https httpbin.org/get a==alfa b==beta

The advantage of using extra call arguments are:

  • more readable call on command line

  • no need to urlencode the values in url

  • with @ there is an option to read the value from a file (see example later on)

Hint: The == might be interpreted as “use it for those things (query parameters) which make the url much longer.”

Header parameters

With HTTPie, headers can be specified using : header name/value parts, e.g.:

$ https httpbin.org/get HeaderA:ValueA HeaderB:ValueB

Reading value from a file using @

Having a file value.txt with content Hello Dolly & World !!, you may pass the value into query parameter as follows:

$ https httpbin.org/get greeting==@value.txt
HTTP/1.1 200 OK
Access-Control-Allow-Credentials: true
Access-Control-Allow-Origin: *
Connection: keep-alive
Content-Length: 376
Content-Type: application/json
Date: Sun, 09 Oct 2022 20:06:29 GMT
Server: gunicorn/19.9.0

{
    "args": {
        "greeting": "Hello Dolly & World !!"
    },
    "headers": {
        "Accept": "*/*",
        "Accept-Encoding": "gzip, deflate",
        "Host": "httpbin.org",
        "User-Agent": "HTTPie/3.2.1",
        "X-Amzn-Trace-Id": "Root=1-634329c5-47816b893d739c2a6178d3fd"
    },
    "origin": "185.151.252.82",
    "url": "https://httpbin.org/get?greeting=Hello+Dolly+%26+World+!!"
}

Note, how was the value from the file read and urlencoded before put into actual url.

SPARQL query using HTTP GET

For HTTP endpoints providing HTTP GET SPARQL protocol, we may send SPARQL queries as follows:

Put query into a file

Create a file, e.g. query.sparql with the query, e.g.:

select ?p
where {?p ?s ?o}
LIMIT 2

Send the query via query query parameter

HTTP GET SPARQL protocol allows to specify query via query argument named query:

$ https lod.tamtamresearch.com/sparql query==@query.sparql
HTTP/1.1 200 OK
Accept-Ranges: bytes
Content-Disposition: filename=sparql_2022-10-09_20-15-10Z.txt
Content-Encoding: gzip
Content-Type: application/sparql-results+xml; charset=UTF-8
Date: Sun, 09 Oct 2022 20:15:10 GMT
Server: Virtuoso/07.20.3233 (Linux) x86_64-pc-linux-gnu
Transfer-Encoding: chunked

<sparql xmlns="http://www.w3.org/2005/sparql-results#" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.w3.org/2001/sw/DataAccess/rf1/result2.xsd">
  <head>
    <variable name="p"/>
  </head>
  <results distinct="false" ordered="true">
    <result>
      <binding name="p">
        <uri>http://www.w3.org/2001/XMLSchema#ID</uri>
      </binding>
    </result>
    <result>
      <binding name="p">
        <uri>http://www.w3.org/2001/XMLSchema#date</uri>
      </binding>
    </result>
  </results>
</sparql>

As we can see, the response (which may differ according to data content queried) is returned in default format with content type application/sparql-results+xml

Ask for results in specific formats

With HTTP we may send a header called Accept which declares preferred response format.

In case of SPARQL endpoint we may use it to get query results in different formats.

Use text/turtle to get the result in turtle format (this time we will omit response headers and show only response body):

$ https --body lod.tamtamresearch.com/sparql \
query==@query.sparql Accept:text/turtle
@prefix res: <http://www.w3.org/2005/sparql-results#> .
@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
_:_ a res:ResultSet .
_:_ res:resultVariable "p" .
@prefix xsd:    <http://www.w3.org/2001/XMLSchema#> .
_:_ res:solution [
      res:binding [ res:variable "p" ; res:value xsd:ID ] ] .
_:_ res:solution [
      res:binding [ res:variable "p" ; res:value xsd:date ] ] .

Note, that for readibility we have used the \ to continue (on Linux shell) with command line arguments on the next line. You can keep the arguments on one line. On Windows, ^ symbol shall work instead of \.

The same may be achieved using format query parameter:

$ https --body lod.tamtamresearch.com/sparql \
query==@query.sparql format==text/turtle
@prefix res: <http://www.w3.org/2005/sparql-results#> .
@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
_:_ a res:ResultSet .
_:_ res:resultVariable "p" .
@prefix xsd:  <http://www.w3.org/2001/XMLSchema#> .
_:_ res:solution [
      res:binding [ res:variable "p" ; res:value xsd:ID ] ] .
_:_ res:solution [
      res:binding [ res:variable "p" ; res:value xsd:date ] ] .

You may try to use various content type values, such as:

  • application/rdf+xml

  • application/sparql-results+json

  • application/sparql-results+xml

  • text/csv

  • text/html

  • text/plain

  • text/tab-separated-values

SPARQL query using HTTP POST

For HTTP endpoints providing HTTP POST SPARQL protocol, we may send SPARQL queries as follows (re-using the SPARQL query in the query.sparql file):

https lod.tamtamresearch.com/sparql content-type:application/sparql-query format==turtle < query.sparql

HTTP POST gets handy at the moment the length of SPARQL query make length of resulting url exceeding available limits.

All alternatives (resulting in exactly the same HTTP request) are:

$ https lod.tamtamresearch.com/sparql content-type:application/sparql-query format==turtle < query.sparql
$ https lod.tamtamresearch.com/sparql content-type:application/sparql-query format==turtle @query.sparql
$ cat query.sparql | https lod.tamtamresearch.com/sparql content-type:application/sparql-query format==turtle

This can be ofcourse combined with different values for the format query parameter or using the Accept header as with HTTP GET.

Previous: SPARQL Query Editor