기초/Linux

curl명령어로 HTTP GET,POST 호출하는 방법

올리버 2022. 2. 3. 17:36

curl 명령어란?

스크립트 또는 쉘상에서 데이터를 전송하기위해 사용되는 command line tool이다.

 

curl은 다양한 프로토콜을 지원한다.

(HTTP, HTTPS, IMAP, FTP, FTPS, POP3, MQTT, SMTP 등등)

 

curl 사용을 위한 설치 방법

sudo apt-get install curl

 

HTTP 호출 방법

URL호출은 다음과 같다.

curl http://test-api.com

HTTP 호출 시 자주 사용하는 옵션은 3가지이다.

-d, --data : Post Request시 보낼 데이터를 설정한다.

-H, --header : Request시 헤더를 설정한다.

-X, --request : 사용할 Request Method 설정한다.  EX) GET, POST  

 

 💡 -d 옵션을 추가하면 자동적으로 POST로 보내지기 때문에, -X POST 제외 가능  

 

GET 방식

curl -X GET -H "Content-Type: application/json" http://test-api.com?key1=value1&key2=value=2

 

POST방식

#body에 데이터 포함하여 보내는 방식
curl -X POST \
     -H "Content-Type: application/json" \
     -d "key1=value1&key2=value2" \
     http://test-api.com

#Json 타입으로 데이터 보내는 방식
curl -X POST \
     -H "Content-Type: application/json" \
     -d '{"key1":"value1","key2":"value2"}' \
     http://test-api.com

 참고 

 

curl

command line tool and library for transferring data with URLs (since 1998) Time to donate to the curl project? Everything curl is a detailed and totally free book that explains basically everything there is to know about curl, libcurl and the associated pr

curl.se

 

'기초 > Linux' 카테고리의 다른 글

SSH Key 접속 시 password를 요구하는 문제 해결  (0) 2022.01.25