728x90
반응형
SMALL
공식문서 HTTPie 를 보고 작성한 글 입니다.
v.3.2.1
https://httpie.io/docs/cli
Usage
HTTPie Docs
Http [flags] [method] url [item [item]]
http PUT pie.dev/put X-API-Token:123 name=John
- -f form
- Output option
- Offline mode
- Authentication
- File redirected
- Wget style
- Sessions
HTTP Method
- Default
- Body 가 없으면 GET
- 있으면 POST
- Http:// 는 생략가능
- Https 로 시작하면 https://
- 복사 후 사용
2. Body 가 있으면 POST
http pie.dev/post hello=world
- Https 로 시작하면 https://
https example.org
Request URL
Querystarting parameters
- Param==value
- File==@files/text.txt
http https://api.github.com/search/repositories q==httpie per_page==1
Shortcut for localhost
- :8080 -> localhost:8080
- : -> localhost (80포트는 생략됨)
http :3000/bar
Other default schemes
- 프로토콜 별칭
alias http-unix='http --default-scheme="http+unix"'
- Path-as-is
http --path-as-is -v example.org/./../../etc/password
Request items
- Key:value - header
- Key=value - data (json, form)
- Key:=value - raw data
- Key==value - query parameter
- —multipart 로 multipart/form-data 로 직렬화
- 파일: —form, -f, —multipart 에만 가능
- —multipart 로 파일존재 결과를 반환
Request Items
File based eparators
- :@ -
- ==@
- =@
http POST pie.dev/post \
X-Data:@files/text.txt # Read a header from a file
token==@files/text.txt # Read a query parameter from a file
name=@files/text.txt # Read a data field’s value from a file
bookmarks:=@files/data.json # Embed a JSON object from a file
Escaping rules
- Value 에 띄어쓰기 있을 때 작은 따옴표
- 필드이름이나 헤더에 - 로 시작한다면 — -이름 과 같이 이스케이핑
http pie.dev/post -- -name-starting-with-dash=foo -Unusual-Header:bar
JSON
Defalut behavior
- Content-type: application/json
- Accept: pplication/json,/;q=0.5
Explicit
- —json 이나 -j 사용해서 accept 를 application/json 으로 세팅
- Httpie 는 자동으로 content-type 과 상관없이 json 데이터를 구분한다.
Non-strict json fields
- := 를 사용해 number boolean array object
- :=@ json file
- =@ 텍스트 파일을 보낼 수 있다
- 배열, 객체는 작은따옴표로 감싼다
- Form 이나 multipart 라면 string float number 는 여전히 가능
- 입력
http PUT pie.dev/put \
name=John \ # String (default)
age:=29 \ # Raw JSON — Number
married:=false \ # Raw JSON — Boolean
hobbies:='["http", "pies"]' \ # Raw JSON — Array
favorite:='{"tool": "HTTPie"}' \ # Raw JSON — Object
bookmarks:=@files/data.json \ # Embed JSON file
description=@files/text.txt # Embed text file
- 출력
PUT /person/1 HTTP/1.1
Accept: application/json, */*;q=0.5
Content-Type: application/json
Host: pie.dev
{
"age": 29,
"hobbies": [
"http",
"pies"
],
"description": "John is a nice guy who likes pies.",
"married": false,
"name": "John",
"favorite": {
"tool": "HTTPie"
},
"bookmarks": {
"HTTPie": "https://httpie.org",
}
}
Nested json
- 객체와 배열을 표현하는 또 다른 방법
- 입력
http pie.dev/post \
platform[name]=HTTPie \
platform[about][mission]='Make APIs simple and intuitive' \
platform[about][homepage]=httpie.io \
platform[about][homepage]=httpie.io \
platform[about][stars]:=54000 \
platform[apps][]=Terminal \
platform[apps][]=Desktop \
platform[apps][]=Web \
platform[apps][]=Mobile
- 출력
{
"platform": {
"name": "HTTPie",
"about": {
"mission": "Make APIs simple and intuitive",
"homepage": "httpie.io",
"stars": 54000
},
"apps": [
"Terminal",
"Desktop",
"Web",
"Mobile"
]
}
}
Introduction
- 배열에 인덱스를 넣어도 된다.
- 빈 인덱스는 null
- Embed raw json 과 nested structure 를 함께 써도 된다.
- 입력
http --offline --print=B pie.dev/post \
category=tools \
search[type]=platforms \
search[platforms][]=Terminal \
search[platforms][1]=Desktop \
search[platforms][3]=Mobile
- 출력
{
"category": "tools",
"search": {
"platforms": [
"Terminal",
"Desktop",
null,
"Mobile"
],
"type": "platforms"
}
}
- 함께 쓰는 예시
http --offline --print=B pie.dev/post \
category=tools \
search[type]=platforms \
'search[platforms]:=["Terminal", "Desktop"]' \
search[platforms][]=Web \
search[platforms][]=Mobile
Advanced usage
- 객체 대신 배열을 보내고싶으면 시작하는 키를 생략
Escaping behavior
- 백슬래쉬로 대괄호 이스케이핑
- 백슬래쉬
- 대괄호 안의 숫자에 하나의 백슬래쉬
- 입력
http --offline --print=B pie.dev/post \
'foo\[bar\]:=1' \
'baz[\[]:=2' \
'baz[\]]:=3'
- 출력
{
"baz": {
"[": 2,
"]": 3
},
"foo[bar]": 1
}
- 입력
http --offline --print=B pie.dev/post \
'object[\1]=stringified' \
'object[\100]=same' \
'array[1]=indexified'
- 출력
{
"array": [
null,
"indexified"
],
"object": {
"1": "stringified",
"100": "same"
}
}
Guiding syntax errors
- 대괄호 missing
HTTPie Syntax Error: Expecting ']'
foo[baz][quux
^
- Type 체크
http --offline --print=B pie.dev/post \
'array[]:=1' \
'array[]:=2' \
'array[key]:=3'
HTTPie Type Error: Can't perform 'key' based access on 'array' which has a type of 'array' but this operation requires a type of 'object'.
array[key]
^^^^^
Raw json
- ?
Forms
Forms
- -f 옵션
- Config 파일러 디폴트 설정
File upload forms
- 파일필드가 하나 이상 존재할 때 content-type 이 바뀜
- @ 와 =@ 차이
- 파일 없이 요청하려면 —multipart
- 파일업로드는 큰 파일의 메모리이슈를 피하기 위해 항상 스트림화 된다.
- Boundary ?
- 파일필드가 하나 이상 존재할 때 content-type 이 바뀜
http -f POST pie.dev/post name='John Smith' cv@~/files/data.xml
<form enctype="multipart/form-data" method="post" action="http://example.com/jobs">
<input type="text" name="name" />
<input type="file" name="cv" />
</form>
Http headers
- Default 헤더가 있다.
- :@ 로 파일의 헤더를 불러온다.
- Default 헤더
GET / HTTP/1.1
Accept: */*
Accept-Encoding: gzip, deflate
User-Agent: HTTPie/<version>
Host: <taken-from-URL>
Empty header and header un-setting
- Header: 로 디폴트 헤더 지울 수 있다.
- ‘Header;’ 로 빈 value 보낼 수 있다.
- 헤더 비우기
http pie.dev/headers 'Header;'
Limiting response headers
- —max-headers=숫자 로 헤더 수 조정
http --max-headers=100 pie.dev/get
Offline 모드
- —offline 으로 요청을 보내지 않는다.
http --offline POST server.chess/api/games API-Key:ZZZ w=magnus b=hikaru t=180 i=2
requst.http 요청을 파일로 만든다.
- —print=HB 의 효과가 있다. (Requst header, body)
# 1. save a raw request to a file:
http --offline POST pie.dev/post hello=world > request.http
Cookies
- Cookie:key=value
- 여러개의 쿠키는 작은따옴표 안, 세미콜론으로 구분
- 하나의 쿠키
http pie.dev/cookies Cookie:sessionid=foo
- 둘 이상의 쿠키
http pie.dev/cookies 'Cookie:sessionid=foo;another-cookie=bar'
Authentication
- Basic 과 digest 지원
- —Auth or -a username:password 로 보낸다.
- —auth-type or -A basic, digest, bearer 혹은 당신이 이미 깐 auth plugins. 디폴트는 basic 이다.
- Https -A bearer -a 토큰 url
- 비밀번호를 생략 시 안전하게 실행
- Basic auth
http -a username:password pie.dev/basic-auth/username/password
- Bearer auth
https -A bearer -a token pie.dev/bearer
Empty password
- ~/.netrc 가 디폴트honored 된다.
- —ifnore-netrc 로 무시할 수 있다.
- .netrc 파일
cat ~/.netrc
machine pie.dev
login httpie
password test
- 성공
http pie.dev/basic-auth/httpie/test
HTTP/1.1 200 OK
[...]
- 실패
http --ignore-netrc pie.dev/basic-auth/httpie/test
HTTP/1.1 401 UNAUTHORIZED
[...]
Auth plugins
- Python Package Index 에서 더 많은 인증 메카니즘을 찾을 수 있다.
- httpie-api-auth: ApiAuth
- httpie-aws-auth: AWS / Amazon S3
- httpie-edgegrid: EdgeGrid
- httpie-hmac-auth: HMAC
- httpie-jwt-auth: JWTAuth (JSON Web Tokens)
- httpie-negotiate: SPNEGO (GSS Negotiate)
- httpie-ntlm: NTLM (NT LAN Manager)
- httpie-oauth1: OAuth 1.0a
- requests-hawk: Hawk
Http redirects
- 보통 리다이렉트 안된다.
Follow location
- —follow or -F 는 바디가 없는 get 요청으로 마지막 요청을 보여준다.
http --follow pie.dev/redirect/3
Showing intermediary redirect responses
- 즉각적인 요청을 보려면 -all 요청
http --follow --all pie.dev/redirect/3
Limiting maximum redirects followed
- 디폴트 리다이렉트 리밋은 30
- —max-redirects=숫자
http --follow --all --max-redirects=2 pie.dev/redirect/3
Proxies
- —proxy ?
http --proxy=http:http://10.10.1.10:3128 --proxy=https:https://10.10.1.10:1080 example.org
- Basic 인증과 함께
http --proxy=http:http://user:pass@10.10.1.10:3128 example.org
Environment variables
- All_porxy, http_proxy, https_proxy 로 Proxies 를 설정할 수 있다.
- No_proxy 로 설정을 제거할 수 있다.
- ~/.bash_profile 에 export HTTP://10.10.1.10:3128 와 같이
- .bash_profile
export HTTP_PROXY=http://10.10.1.10:3128
export HTTPS_PROXY=https://10.10.1.10:1080
export NO_PROXY=localhost,example.com
Socks
- 프록시처럼 써라
http --proxy=http:socks5://user:pass@host:port --proxy=https:socks5://user:pass@host:port example.org
Https
Several ssl certificate verification
- 호스트의 ssl 인증을 스킵하기 위해 —verify=no
- 디폴트는 yes 이다.
- SSL 인증 스킵
http --verify=no https://pie.dev/get
Custom ca bundle
- —verify=
http --verify=/ssl/custom_ca_bundle https://example.org
Client side ssl certificate
- —cert=
- —cert-key=
- Prompt 를 보고싶지 않다면 —cert-key-pass=
Ssl version
# Specify the vulnerable SSL v3 protocol to talk to an outdated server:
http --ssl=ssl3 https://vulnerable.example.org
여기까지 v.3.2.1
... 문서 작성 중입니다.
728x90
반응형
LIST