CURL

권보원·2023년 8월 7일
0

CURL이란

URL 기반으로 데이터를 웹(서버)로 전송하기 위한 명령줄 유틸리티다.
별도의 view나 툴 업시 직접 서버에 http request를 날리고 response를 확인 할 수 있다.
curl을 사용하면 HTTP, HTTPS, SCP, SFTP, FRP 등 다양한 프로토콜과 Proxy, Header, Cookie등 세부 옵션까지 쉽게 설정 할 수 있다.

CURL 설치

현재 대부분의 리눅스 배포 환경에는 curl패키지가 미리 설치되어 있다.
시스템에 curl 패키지 설치되어 있는지 확이낳려면 콘솔을 열고 curl 을 입력하면 된다. curl이 설치된 경우 시스템에서 curl을 인쇄한다.
curl이 없으면 "sudo apt install curl"로 설치하면 된다.

CURL 사용법

curl [option][URL]
가장 감단한 형태는 'curl example.com'이다.
프로토콜을 지정하지 않은 경우 , curl은 사용할 프로토콜을 추측하려고 시도하며, 이 프로토콜은 HTTP로 기본 설정된다.

-X: http method를 get/post로 할건지 지정, 디폴트는 get
-H: http 헤더값
-d: http body값 or 함께 전달할 파라미터 값
-G: 전송할 사이트 url 및 ip 주소
-I: 사이트의 Header 정보만 가져오기
-i: 사이트의 Header와 바디 정보를 함께 가져오기
-u: 사용자의 정보

  • 요청 시 자세한 정보 표시
    curl -v [URL]
  • post 요청 보내기
    curl -X POST [URL]
  • data 같이 보내기
    curl -X POST --data "name=john&age=10" [URL]
  • form 보내기
    curl -d name="John Grib" -d hobby="coding" [URL]
  • json 보내기
    curl -d '{"name":"john"}' -H "Content-Type:application/json" [URL]
  • 출력을 파일에 저장
  • curl 명령의 결과를 저장하려면 -o 또는 -O옵션 사용
  • 소문자 -o: 미리 정의된 파일 이름을 사용하여 파일을 저장한다.
  • curl -o [저장할 파일 이름] https:// [접근할 URL]
  • 대문자 -O: 파일을 원래 파일 이름으로 저장한다.
  • curl -O https:// [접근할 URL]
  1. Local - post
    $ curl -X POST localhost:3000/apis/search?v=0.3.0 -H "Content-Type: application/json" -d '{"searchId": "test999991", "restaurantMenus": ["test1","test2","test3","test4"]}'
  1. Local - get
    $ curl -G localhost:3000/apis/search? -H 'Content-type:application/json' -d 'v=0.3.0' -d 'f=finder' -d 'searchId=test999991’ -v
  1. Server - post
    $ curl -X POST https://naver.com/apis/search?v=0.3.0 -H "Content-Type: application/json" -d '{"searchId": "test6161990", "restaurantMenus": ["test1","test2","test3","test4"]}'
  1. Server - get
    $ curl -G https://naver.com/apis/search? -H 'Content-type:application/json' -d 'v=0.3.0' -d 'f=finder' -d 'searchId=test6161990' -v

출처:https://6161990src.tistory.com/120

2개의 댓글

comment-user-thumbnail
2023년 8월 7일

큰 도움이 되었습니다, 감사합니다.

답글 달기
comment-user-thumbnail
2023년 8월 12일

권쨩 화이팅!!

답글 달기