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


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시 보낼 데이터를 설정한다.
    • -d 옵션을 추가하면 자동적으로 POST로 보내지기 때문에, -X POST 제외 가능
  • -H, –header : Request시 헤더를 설정한다.
  • -X, –request : 사용할 Request Method 설정한다.
    • EX) GET, 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

참고