Skip to content

Instantly share code, notes, and snippets.

@LintangWisesa
Created January 10, 2023 09:27
Show Gist options
  • Select an option

  • Save LintangWisesa/d40fe71881f3794c1ea2e1936c354b84 to your computer and use it in GitHub Desktop.

Select an option

Save LintangWisesa/d40fe71881f3794c1ea2e1936c354b84 to your computer and use it in GitHub Desktop.
HTTPie Tutorial

simplinnovation

HTTPie Tutorial

Video

HTTPie is a command-line HTTP client. Its goal is to make CLI interaction with web services as human-friendly as possible. HTTPie is designed for testing, debugging, and generally interacting with APIs & HTTP servers. The http & https commands allow for creating and sending arbitrary HTTP requests. They use simple and natural syntax and provide formatted and colorized output.

Installation

  • PyPI (Python 3.7+): $ pip install httpie
  • Homebrew: $ brew install httpie
  • MacPorts: $ port install httpie
  • Chocolatey (Win64): $ choco install httpie
  • Snapcraft: $ snap install httpie
  • Linuxbrew: $ brew install httpie
  • Debian & Ubuntu: $ apt install httpie
  • CentOS & RHEL: $ yum install httpie
  • Fedora: $ dnf install httpie
  • archLinux: $ pacman -Syu httpie
  • FreeBSD: $ pkg install www/py-httpie

Basic Commands

Command Fungsi
$ http --version or $ https --version Cek versi HTTPie
$ http --help or $ https --help Menampilkan menu bantuan
$ http --manual or $ https --manual Menampilkan manual penggunaan

HTTP GET Request

Command Fungsi
$ http https://fakestoreapi.com/products GET request ke endpoint /products
$ http https://fakestoreapi.com/products/2 GET request ke endpoint /products/2
$ https ://fakestoreapi.com/products/2 GET request ke endpoint /products/2
$ https fakestoreapi.com/products/2 GET request ke endpoint /products/2
$ https get fakestoreapi.com/products/2 GET request ke endpoint /products/2

By default response yang ditampilkan HTTPie untuk setiap request adalah response header line, response header dan response body seperti berikut:

HTTP/1.1 200 OK
Access-Control-Allow-Origin: *
CF-Cache-Status: DYNAMIC
CF-RAY: 787404078ad3880d-SIN
Connection: keep-alive
Content-Encoding: gzip
Content-Type: application/json; charset=utf-8
Date: Tue, 10 Jan 2023 08:24:21 GMT
Etag: W/"239-bLDSJo+O3FjSlzUr879oE3z3US8"
NEL: {"success_fraction":0,"report_to":"cf-nel","max_age":604800}
Report-To: {"endpoints":[{"url":"https:\/\/a.nel.cloudflare.com\/report\/v3?s=sJz7PitFdOlkPOpzyVos%2FAL5VCtU0RJZrd%2FfIR7H%2FjHA%2F9twcJgxUT3CCxJNXdhwZWSorYY47G3J6CIaRsxADYZcBnVX0SqmDxdyGWvhqCAfY%2BzX5PymNg1Q4G5mUunS0k3s"}],"group":"cf-nel","max_age":604800}
Server: cloudflare
Transfer-Encoding: chunked
X-Powered-By: Express
alt-svc: h3=":443"; ma=86400, h3-29=":443"; ma=86400

{
    "category": "men's clothing",
    "description": "Slim-fitting style, contrast raglan long sleeve, three-button henley placket, light weight & soft fabric for breathable and comfortable wearing. And Solid stitched shirts with round neck made for durability and a great fit for casual fashion wear and diehard baseball fans. The Henley style round neckline includes a three-button placket.",
    "id": 2,
    "image": "https://fakestoreapi.com/img/71-3HjGNDUL._AC_SY879._SX._UX._SY._UY_.jpg",
    "price": 22.3,
    "rating": {
        "count": 259,
        "rate": 4.1
    },
    "title": "Mens Casual Premium Slim Fit T-Shirts "
}

Print & Output Options

Command Fungsi
$ https get fakestoreapi.com/products/2 -h or --headers GET request ke endpoint /products/2 dan hanya menampilkan header response saja.
$ https get fakestoreapi.com/products/2 -b or --body GET request ke endpoint /products/2 dan hanya menampilkan body response saja.
$ https get fakestoreapi.com/products/2 -m or --meta GET request ke endpoint /products/2 dan hanya menampilkan meta response saja.
$ https get fakestoreapi.com/products/2 -v GET request ke endpoint /products/2 dan menampilkan request & response secara lengkap.
$ https get fakestoreapi.com/products/2 -vv GET request ke endpoint /products/2 dan menampilkan request, response dan meta secara lengkap.
$ https get fakestoreapi.com/products/2 -pH GET request ke endpoint /products/2 dan hanya menampilkan header request saja, dapat pula dinyatakan sebagai -p=H, --print H atau --print=H.
$ https get fakestoreapi.com/products/2 -pB GET request ke endpoint /products/2 dan hanya menampilkan body request saja, dapat pula dinyatakan sebagai -p=B, --print B atau --print=B.
$ https get fakestoreapi.com/products/2 -ph GET request ke endpoint /products/2 dan hanya menampilkan header response saja, dapat pula dinyatakan sebagai -p=h, --print h atau --print=h.
$ https get fakestoreapi.com/products/2 -pb GET request ke endpoint /products/2 dan hanya menampilkan body response saja, dapat pula dinyatakan sebagai -p=b, --print b atau --print=b.
$ https get fakestoreapi.com/products/2 -pm GET request ke endpoint /products/2 dan hanya menampilkan meta response saja, dapat pula dinyatakan sebagai -p=m, --print m atau --print=m.
$ https get fakestoreapi.com/products/2 -pHbm GET request ke endpoint /products/2 dan hanya menampilkan header request, body response dan meta response saja.

Querystring Parameters

Command Fungsi
$ https https://fakestoreapi.com/products limit==5 GET request ke endpoint /products/2 dengan query parameter limit=5. Intinya, seperti melakukan request ke https://fakestoreapi.com/products?limit=5.
$ https https://fakestoreapi.com/products sort==desc GET request ke endpoint /products/2 dengan query parameter sort=desc. Intinya, seperti melakukan request ke https://fakestoreapi.com/products?sort=desc.
$ https https://fakestoreapi.com/products limit==5 sort==desc GET request ke endpoint /products/2 dengan query parameter limit=5 dan sort=desc. Intinya, seperti melakukan request ke https://fakestoreapi.com/products?limit=5&sort=desc.

Download Files

Command Fungsi
$ https get https://fakestoreapi.com/img/abc.jpg --download Mendownload file abc.jpg dari https://fakestoreapi.com/img.
$ https get https://fakestoreapi.com/img/abc.jpg -d Mendownload file abc.jpg dari https://fakestoreapi.com/img.

Request Header

Default request header yang disertakan saat melakukan request dengan menggunakan HTTPie adalah:

Accept: */*
Accept-Encoding: gzip, deflate
Connection: keep-alive
Host: fakestoreapi.com
User-Agent: HTTPie/3.2.1
Command Fungsi
$ https get fakestoreapi.com/products/2 Secret-Key:12345 -pH Menyertakan header request Secret-Key:12345 saat melakukan GET request ke /products/2.
$ https get fakestoreapi.com/products/2 Secret-Key:12345 Auth:abcde -pH Menyertakan header request Secret-Key:12345 dan Auth:abcde saat melakukan GET request ke /products/2.
$ https get fakestoreapi.com/products/2 Secret-Key:12345 Auth:"Bearer abcde" -pH Menyertakan header request Secret-Key:12345 dan Auth:Bearer abcde saat melakukan GET request ke /products/2.
$ https get fakestoreapi.com/products/2 Secret-Key:12345 "Auth:Bearer abcde" -pH Menyertakan header request Secret-Key:12345 dan Auth:Bearer abcde saat melakukan GET request ke /products/2.
$ https get fakestoreapi.com/products/2 Cookie:cookieA=12345 -pH Menyertakan header request Cookie:cookieA=12345 saat melakukan GET request ke /products/2.
$ https get fakestoreapi.com/products/2 Cookie:cookieA=12345;cookieB=67890 -pH Menyertakan header request Cookie:cookieA=12345; cookieB=67890 saat melakukan GET request ke /products/2.
$ https get fakestoreapi.com/products/2 "Cookie: cookieA=12345; cookieB=67890" -pH Menyertakan header request Cookie:cookieA=12345; cookieB=67890 saat melakukan GET request ke /products/2.

Authentication

HTTPie support 2 metode authentication dasar: Basic dan Bearer token. Pada dasarnya 2 metode ini hanya menambahkan header request Authorization dengan value tertentu.

Basic Auth

Authorization: base64(<username>:<password>)

Bearer Token Auth

Authorization: Bearer <token>
Command Fungsi
$ https get fakestoreapi.com/products/2 --auth-type basic --auth lintang:12345 -pH Menyertakan header request Authorization:"Basic bGludGFuZzoxMjM0NQ==" saat melakukan GET request ke /products/2. HTTPie melakukan base64 encoding pada value lintang:12345 secara otomatis.
$ https get fakestoreapi.com/products/2 -A basic -a lintang:12345 -pH Menyertakan header request Authorization:"Basic bGludGFuZzoxMjM0NQ==" saat melakukan GET request ke /products/2. HTTPie melakukan base64 encoding pada value lintang:12345 secara otomatis.
$ https get fakestoreapi.com/products/2 --auth-type bearer --auth qujYygda7 -pH Menyertakan header request Authorization:"Bearer qujYygda7" saat melakukan GET request ke /products/2.
$ https get fakestoreapi.com/products/2 -A bearer -a qujYygda7 -pH Menyertakan header request Authorization:"Bearer qujYygda7" saat melakukan GET request ke /products/2.

HTTP POST Request

Command Fungsi
$ http post fakestoreapi.com/products POST request ke endpoint /products.
$ http post fakestoreapi.com/products title=Barang1 POST request ke endpoint /products dengan menyertakan body request JSON: {"title":"Barang1"}
$ http fakestoreapi.com/products title=Barang1 POST request ke endpoint /products dengan menyertakan body request JSON: {"title":"Barang1"}
$ http post fakestoreapi.com/products title=Barang1 category=KategoriA POST request ke endpoint /products dengan menyertakan body request JSON: {"title":"Barang1", "category":"KategoriA"}
$ http post fakestoreapi.com/products title=Barang1 price=12000 POST request ke endpoint /products dengan menyertakan body request JSON: {"title":"Barang1", "price":"12000"}. Note: Value price adalah string "12000", bukan number.

Raw JSON Body Request

Command Fungsi
$ http post fakestoreapi.com/products title=Barang1 price:=12000 POST request ke endpoint /products dengan menyertakan body request JSON: {"title":"Barang1", "price":12000}. Value price adalah number 12000.
$ http post fakestoreapi.com/products title=Barang1 price:=false POST request ke endpoint /products dengan menyertakan body request JSON: {"title":"Barang1", "price":false}. Value price adalah boolean false.
$ http post fakestoreapi.com/products title=Barang1 price:=\"12000\" POST request ke endpoint /products dengan menyertakan body request JSON: {"title":"Barang1", "price":"12000"}. Value price adalah string "12000".
$ http post fakestoreapi.com/products title=Barang1 price:=[\"apel\",\"belimbing\",\"ceri\"] POST request ke endpoint /products dengan menyertakan body request JSON: {"title":"Barang1", "price":["apel", "belimbing", "ceri"]}. Value price adalah array berisi kumpulan string.
$ http post fakestoreapi.com/products title=Barang1 price:={\"name\":\"Andi\"} POST request ke endpoint /products dengan menyertakan body request JSON: {"title":"Barang1", "price":{"name":"Andi"}}. Value price adalah object.

HTTP PUT, PATCH & DELETE Request

Command Fungsi
$ http put fakestoreapi.com/products/7 -pb PUT request ke /products/7
$ http patch fakestoreapi.com/products/7 -pb PATCH request ke /products/7
$ http delete fakestoreapi.com/products/7 -pb DELETE request ke /products/7

๐Ÿ” Lintang Wisesa


lintang ymail lintang facebook lintang twitter lintang youtube lintang linkedin lintang github lintang hackster lintang bio
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment