-
curl 명령어의 아규먼트Linux 2025. 3. 11. 20:25728x90
curl 명령어는 데이터를 전송하거나 가져오는 데 사용되는 강력한 CLI 도구입니다. HTTP, HTTPS, FTP, SFTP, SCP 등 다양한 프로토콜을 지원하며, 여러 옵션(argument)을 활용해 요청을 세밀하게 조정할 수 있습니다.
🎯 요약
옵션설명
-X HTTP 메서드 지정 -H 요청 헤더 추가 -d 데이터 전송 -F 파일 업로드 -u 기본 인증 (username:password) -i 응답 헤더 포함 -I 응답 헤더만 출력 -L 리디렉션 따라가기 -o 파일 저장 (이름 지정) -O 파일 저장 (원래 이름 유지) -s 진행 상태 숨기기 -v 디버깅 모드 (자세한 요청/응답 로그 출력) 이제 curl을 활용해서 다양한 HTTP 요청을 효율적으로 다룰 수 있습니다!
🛠 주요 아규먼트 (옵션)
1️⃣ 기본 요청
- curl <URL>
기본적으로 GET 요청을 수행합니다.-
curl https://example.com
-
- -X, --request <METHOD>
HTTP 요청 메서드를 지정합니다. -
2️⃣ 요청 헤더 설정
- -H, --header "<header>: <value>"
HTTP 요청 헤더를 추가합니다.- curl -H "Authorization: Bearer TOKEN" -H "Content-Type: application/json" https://api.example.com
3️⃣ 데이터 전송
- -d, --data "<data>"
요청 바디에 데이터를 포함시킵니다. 기본적으로 POST 요청을 수행합니다.- curl -d "username=test&password=1234" -X POST https://example.com/login
- JSON 데이터 전송:
- curl -X POST -H "Content-Type: application/json" -d '{"name": "Simon"}' https://example.com/api
- --data-urlencode "<data>"
데이터를 URL 인코딩하여 전송합니다.-
curl --data-urlencode "query=hello world" https://example.com/search
-
- -F, --form "<name>=<value>"
multipart/form-data 형식으로 파일 업로드를 할 때 사용됩니다.-
curl -F "file=@myfile.txt" https://example.com/upload
-
4️⃣ 인증 관련
- -u, --user "<username>:<password>"
기본 인증(Basic Auth)으로 요청을 보냅니다.-
curl -u admin:password123 https://example.com
-
- --oauth2-bearer "<TOKEN>"
OAuth 2.0 토큰을 사용한 인증 요청을 보냅니다.-
curl --oauth2-bearer TOKEN123 https://api.example.com
-
5️⃣ 응답 제어
- -i, --include
응답 헤더를 포함하여 출력합니다.-
curl -i https://example.com
-
- -I, --head
HTTP 응답의 헤더만 가져옵니다.-
curl -I https://example.com
-
- -L, --location
리디렉션(302, 301 응답)을 자동으로 따라갑니다.-
curl -L https://short.url/redirect
-
6️⃣ 출력 제어
- -o, --output <filename>
응답을 파일로 저장합니다.-
curl -o page.html https://example.com
-
- -O
원래 파일 이름으로 저장합니다.-
curl -O https://example.com/file.zip
-
- -s, --silent
진행 상태 출력 없이 조용히 실행합니다.-
curl -s https://example.com
-
- -v, --verbose
요청 및 응답의 상세 정보를 출력합니다.-
curl -v https://example.com
-
7️⃣ 기타 옵션
- --max-time <seconds>
요청 제한 시간을 설정합니다.-
curl --max-time 10 https://example.com
-
- --retry <n>
요청 실패 시 최대 <n>번 재시도합니다.-
curl --retry 3 https://example.com
-
🛠 실전 예제
✅ 1. GET 요청
curl -X GET https://jsonplaceholder.typicode.com/posts/1✅ 2. POST 요청 (JSON 데이터)
curl -X POST -H "Content-Type: application/json" -d '{"title": "Hello", "body": "World"}' https://jsonplaceholder.typicode.com/posts✅ 3. 파일 다운로드
curl -O https://example.com/sample.zip✅ 4. 파일 업로드
curl -F "file=@report.pdf" https://example.com/upload✅ 5. Bearer 토큰을 이용한 API 요청
curl -H "Authorization: Bearer YOUR_TOKEN_HERE" https://api.example.com/data728x90'Linux' 카테고리의 다른 글
TLS 서버 이름 표시의 작동 방식인 SNI란? (0) 2025.03.10 EOF(End Of File) stdin 활용 (0) 2024.08.13 - curl <URL>