cURL(client URL) 은 API를 빠르게 호출하고자 할 때 유용하다. 상황에 따라서 PostMan과 같은 API 호출 앱보다 빠를 수 있다. 물론 Swagger 가 있다면 굳이 사용할 필요는 없다. 항상 Swagger 가 존재한다는 보장이 없으니, 자주 사용하는 cURL 명령어는 익혀 두는 게 편할 것 같다.
GET
curl -H 'X-Auth-Token: token..' \
-X GET \
https://api-xxxx.co.kr/user
POST
curl -d 'name=jaeho&age=20' \
-X POST /
https://api-jaeho.co.kr/user
\ 는 개행을 뜻한다.
-X GET, -X POST 는 생략 가능하다. -d 가 있느냐 없느냐로 POST 와 GET을 구분하는 것으로 예상된다.
Request body 를 아래와 같이 구분하기도 한다.
curl -d name=jaeho \
-d age=20 \
https://api-xxxx.co.kr/user
Request Body 부분을 json으로 전송할 때는 json을 그대로 써준다.
curl -d '{"agree":true, age:21}'
https://api-xxxx.co.kr/user
Request Body 부분을 파일로 참조하면 오타 확률이 줄어든다.
curl -d @request.json \
-H 'X-Auth-Token: xxx' \
https://api-xxxx.co.kr/user
여러 개의 헤더는 여러 개의 -H로 구분한다.
curl -H 'Content-Type: application/json' \
-H 'X-Auth-Token: xxx' \
-H 'name: xxxx' \
https://api-xxxx.co.kr/user
참고
https://www.shellhacks.com/curl-add-header-multiple-headers-authorization/
https://linuxize.com/post/curl-rest-api/
https://www.baeldung.com/curl-rest
https://recordsoflife.tistory.com/m/484
cURL로 무엇을 더 할 수 있을까?
날씨를 볼 수 있습니다.
curl wttr.in
curl wttr.in/Seoul
curl wttr.in/Namsan
Weather report: Namsan
Haze
_ - _ - _ - 20 °C
_ - _ - _ ↑ 7 km/h
_ - _ - _ - 9 km
0.0 mm
┌─────────────┐
┌──────────────────────────────┬───────────────────────┤ Tue 24 May ├───────────────────────┬──────────────────────────────┐
│ Morning │ Noon └──────┬──────┘ Evening │ Night │
├──────────────────────────────┼──────────────────────────────┼──────────────────────────────┼──────────────────────────────┤
│ \ / Sunny │ \ / Sunny │ \ / Sunny │ \ / Clear │
│ .-. +23(24) °C │ .-. +30(29) °C │ .-. 26 °C │ .-. +22(24) °C │
│ ― ( ) ― ↗ 4-5 km/h │ ― ( ) ― → 13-14 km/h │ ― ( ) ― → 15-17 km/h │ ― ( ) ― → 5-7 km/h │
│ `-’ 10 km │ `-’ 10 km │ `-’ 10 km │ `-’ 10 km │
│ / \ 0.0 mm | 0% │ / \ 0.0 mm | 0% │ / \ 0.0 mm | 0% │ / \ 0.0 mm | 0% │
└──────────────────────────────┴──────────────────────────────┴──────────────────────────────┴──────────────────────────────┘
Location: 남산, 서울, 04340, 대한민국 [37.5524374,126.988433]
옵션을 보면 여러가지 기능이 있어요.
curl wttr.in -h
Usage: curl [options...] <url>
-d, --data <data> HTTP POST data
-f, --fail Fail silently (no output at all) on HTTP errors
-h, --help <category> Get help for commands
-i, --include Include protocol response headers in the output
-o, --output <file> Write to file instead of stdout
-O, --remote-name Write output to a file named as the remote file
-s, --silent Silent mode
-T, --upload-file <file> Transfer local FILE to destination
-u, --user <user:password> Server user and password
-A, --user-agent <name> Send User-Agent <name> to server
-v, --verbose Make the operation more talkative
-V, --version Show version number and quit
동일한 커맨드로 브라우저에서도 날씨를 볼 수 있어요.
https://wttr.in/