curl은 명령줄에서 HTTP 요청을 보내고 응답을 받는 강력한 도구입니다. API 호출, 파일 다운로드, 데이터 업로드 및 디버깅에 유용하게 사용할 수 있습니다. 이 글에서는 curl의 기본 개념부터 고급 사용법까지 단계별로 자세히 설명하겠습니다.
1. curl이란?
curl은 “Client URL”의 약자로, 다양한 프로토콜(HTTP, HTTPS, FTP, SCP, SFTP 등)을 지원하는 명령줄 기반의 데이터 전송 도구입니다. curl을 사용하면 웹 서버와 쉽게 상호 작용할 수 있으며, 스크립트 및 자동화 작업에서도 많이 활용됩니다.
2. curl 기본 사용법
curl 명령어는 기본적으로 GET 요청을 수행합니다.
curl <URL>
예제: 웹 페이지 요청하기
curl https://example.com
위 명령어를 실행하면 example.com의 HTML 소스 코드가 출력됩니다.
3. HTTP 요청 방식 지정하기 (-X 옵션)
기본적으로 curl은 GET 요청을 보냅니다. 다른 HTTP 메서드를 사용하려면 -X 옵션을 활용합니다.
예제: POST 요청 보내기
curl -X POST https://example.com/api
예제: PUT 요청 보내기
curl -X PUT https://example.com/api
예제: DELETE 요청 보내기
curl -X DELETE https://example.com/api
4. HTTP 요청 헤더 추가하기 (-H 옵션)
서버에 특정 요청 헤더를 추가하려면 -H 옵션을 사용합니다.
예제: 사용자 정의 헤더 추가
curl -H "User-Agent: MyCustomClient" https://example.com
예제: 인증 토큰 포함
curl -H "Authorization: Bearer YOUR_ACCESS_TOKEN" https://example.com/api
예제: 여러 개의 헤더 추가
curl -H "Content-Type: application/json" -H "Accept: application/json" https://example.com/api
5. 데이터 전송하기 (-d 옵션)
예제: 폼 데이터 전송 (application/x-www-form-urlencoded)
curl -X POST -d "name=John&age=30" https://example.com/api
예제: JSON 데이터 전송 (application/json)
curl -X POST -H "Content-Type: application/json" -d '{"name":"John","age":30}' https://example.com/api
예제: XML 데이터 전송
curl -X POST -H "Content-Type: application/xml" -d '<user><name>John</name></user>' https://example.com/api
6. 응답 결과 파일로 저장하기 (-o 옵션)
웹 서버의 응답 데이터를 파일로 저장할 수 있습니다.
예제: HTML 페이지 저장
curl -o page.html https://example.com
예제: 이미지 다운로드
curl -o image.jpg https://example.com/image.jpg
예제: 여러 개의 파일 다운로드 (-O 옵션 사용)
curl -O https://example.com/image1.jpg -O https://example.com/image2.jpg
7. 파일 업로드하기 (-F 옵션)
파일을 업로드하려면 -F 옵션을 사용합니다.
예제: 파일 업로드
curl -X POST -F "file=@/path/to/file.txt" https://example.com/upload
예제: 여러 개의 파일 업로드
curl -X POST -F "file1=@/path/to/file1.txt" -F "file2=@/path/to/file2.txt" https://example.com/upload
8. 요청 속도 제한 (--limit-rate 옵션)
속도를 제한하여 데이터를 다운로드할 수 있습니다.
예제: 다운로드 속도 제한 (500KB/s)
curl --limit-rate 500K -O https://example.com/largefile.zip
9. 타임아웃 설정 (--connect-timeout, --max-time 옵션)
네트워크 요청의 타임아웃을 설정할 수 있습니다.
예제: 연결 타임아웃 5초 설정
curl --connect-timeout 5 https://example.com
예제: 전체 요청 타임아웃 10초 설정
curl --max-time 10 https://example.com
10. 병렬 다운로드 (--parallel 옵션)
여러 개의 요청을 동시에 실행할 수도 있습니다.
예제: 병렬 다운로드
curl --parallel --parallel-immediate -O https://example.com/file1.jpg -O https://example.com/file2.jpg
🏆 curl 사용법 요약
| 기능 | 명령어 예제 |
|---|---|
| 기본 GET 요청 | curl https://example.com |
| POST 요청 | curl -X POST https://example.com/api |
| 요청 헤더 추가 | curl -H "User-Agent: CustomClient" https://example.com |
| JSON 데이터 전송 | curl -X POST -H "Content-Type: application/json" -d '{"name":"John"}' https://example.com/api |
| 응답 파일 저장 | curl -o file.html https://example.com |
| 파일 업로드 | curl -X POST -F "file=@file.txt" https://example.com/upload |
| 속도 제한 | curl --limit-rate 500K -O https://example.com/largefile.zip |
| 타임아웃 설정 | curl --max-time 10 https://example.com |
| 병렬 다운로드 | curl --parallel -O https://example.com/file1.jpg -O https://example.com/file2.jpg |
이제 curl을 자유롭게 활용할 수 있습니다! 필요할 때마다 이 가이드를 참고하여 다양한 HTTP 요청을 처리해 보세요. 😊