본문 바로가기

리눅스

[리눅스] httpie 명령어

728x90

httpie 명령어

HTTPie는 명령줄 기반으로 HTTP 요청을 보내고 응답을 받을 수 있는 유용한 도구입니다. 다음은 HTTPie의 설치와 기본 사용 방법을 설명해 드리겠습니다.

 

pip 설치

  • python 버전 확인
$ python --version
Python 2.7.5
  • pip 설치
$ wget https://bootstrap.pypa.io/pip/2.7/get-pip.py
$ python get-pip.py
---output---
Collecting pip<21.0
  Downloading pip-20.3.4-py2.py3-none-any.whl (1.5 MB)
     |████████████████████████████████| 1.5 MB 1.7 MB/s
Collecting setuptools<45
  Downloading setuptools-44.1.1-py2.py3-none-any.whl (583 kB)
     |████████████████████████████████| 583 kB 34.8 MB/s
Collecting wheel
  Downloading wheel-0.36.2-py2.py3-none-any.whl (35 kB)
Installing collected packages: pip, setuptools, wheel
Successfully installed pip-20.3.4 setuptools-44.1.1 wheel-0.36.2
$ which pip
/bin/pip

httpie 설치

pip install --upgrade httpie
$ pip install --upgrade httpie
---output---
Collecting httpie
  Downloading httpie-1.0.3-py2.py3-none-any.whl (59 kB)
     |████████████████████████████████| 59 kB 871 kB/s
Collecting Pygments>=2.3.1
  Downloading Pygments-2.5.2-py2.py3-none-any.whl (896 kB)
     |████████████████████████████████| 896 kB 2.7 MB/s
Collecting requests>=2.21.0
  Downloading requests-2.26.0-py2.py3-none-any.whl (62 kB)
     |████████████████████████████████| 62 kB 261 kB/s
Collecting certifi>=2017.4.17
  Downloading certifi-2021.5.30-py2.py3-none-any.whl (145 kB)
     |████████████████████████████████| 145 kB 11.9 MB/s
Collecting urllib3<1.27,>=1.21.1
  Downloading urllib3-1.26.6-py2.py3-none-any.whl (138 kB)
     |████████████████████████████████| 138 kB 11.7 MB/s
Collecting idna<3,>=2.5; python_version < "3"
  Downloading idna-2.10-py2.py3-none-any.whl (58 kB)
     |████████████████████████████████| 58 kB 2.0 MB/s
Collecting chardet<5,>=3.0.2; python_version < "3"
  Downloading chardet-4.0.0-py2.py3-none-any.whl (178 kB)
     |████████████████████████████████| 178 kB 11.3 MB/s
Installing collected packages: Pygments, certifi, urllib3, idna, chardet, requests, httpie
  Attempting uninstall: chardet
    Found existing installation: chardet 2.2.1
    Uninstalling chardet-2.2.1:
      Successfully uninstalled chardet-2.2.1
Successfully installed Pygments-2.5.2 certifi-2021.5.30 chardet-4.0.0 httpie-1.0.3 idna-2.10 requests-2.26.0 urllib3-1.26.6

HTTPie 사용

  • 옵션
$ http
usage: http [--json] [--form] [--pretty {all,colors,format,none}]
            [--style STYLE] [--print WHAT] [--headers] [--body] [--verbose]
            [--all] [--history-print WHAT] [--stream] [--output FILE]
            [--download] [--continue]
            [--session SESSION_NAME_OR_PATH | --session-read-only SESSION_NAME_OR_PATH]
            [--auth USER[:PASS]] [--auth-type {basic,digest}]
            [--proxy PROTOCOL:PROXY_URL] [--follow]
            [--max-redirects MAX_REDIRECTS] [--timeout SECONDS]
            [--check-status] [--verify VERIFY]
            [--ssl {ssl2.3,ssl3,tls1,tls1.1,tls1.2}] [--cert CERT]
            [--cert-key CERT_KEY] [--ignore-stdin] [--help] [--version]
            [--traceback] [--default-scheme DEFAULT_SCHEME] [--debug]
            [METHOD] URL [REQUEST_ITEM [REQUEST_ITEM ...]]
728x90

 

  • GET 요청
http GET http://example.com
  • POST 요청
http POST http://example.com/post username='john' password='secret'
  • 요청 헤더 설정
http GET http://example.com 'Authorization:Bearer Token'
  • JSON 데이터 전송
http POST http://example.com/post Content-Type:application/json name='John' age:=30
  • 파일 업로드
http POST http://example.com/upload file@/path/to/file.jpg
  • 응답 데이터 파싱
http GET http://example.com --json | jq '.data'

위의 예시들은 HTTPie의 일부 기능을 보여주기 위한 것입니다. HTTPie는 다양한 기능과 옵션을 제공하므로, 더 많은 사용 방법과 옵션에 대해서는 공식 문서(https://httpie.io/docs)를 참조하시기 바랍니다.

 

HTTPie를 사용하면 명령줄에서 간편하게 HTTP 요청을 수행하고 응답을 확인할 수 있으므로, API 테스트, 디버깅 및 웹 서비스와의 상호작용에 유용합니다.

 

http -v --follow sangchul.kr
$ http -v --follow sangchul.kr
GET / HTTP/1.1
Accept: */*
Accept-Encoding: gzip, deflate
Connection: keep-alive
Host: sangchul.kr
User-Agent: HTTPie/1.0.3

HTTP/1.1 302 Found
Cache-Control: no-cache
Content-length: 0
Location: https://sangchul.kr/

GET / HTTP/1.1
Accept: */*
Accept-Encoding: gzip, deflate
Connection: keep-alive
Host: sangchul.kr
User-Agent: HTTPie/1.0.3

HTTP/1.1 200 OK
Access-Control-Allow-Origin: https://sangchul.kr
Content-Encoding: gzip
Content-Length: 11771
Content-Type: text/html; charset=utf-8
Date: Tue, 27 Jul 2021 07:02:49 GMT
P3P: CP='ALL DSP COR MON LAW OUR LEG DEL'
Vary: Accept-Encoding
X-UA-Compatible: IE=Edge

<!doctype html>
<html lang="ko">
...

 

참고URL

- github : https://github.com/httpie/httpie

 

728x90