Web Programming
Curl 자주 사용하는 커맨드 예제 및 옵션( Json payload, POST, GET 등 )
맑은안개
2023. 2. 8. 15:14
1. URL 헤더정보
$ curl -I www.naver.com
HTTP/1.1 302 Moved Temporarily
Server: NWS
Content-Type: text/html
Location: https://www.naver.com/
Date: Wed, 08 Feb 2023 06:03:35 GMT
Connection: keep-alive
2. 응답 body 출력
curl https://www.naver.com
<중략>
ata-clk="helpcenter">고객센터</a></li> </ul> <address class="addr"><a href="https://www.navercorp.com" target="_blank" data-clk="nhn">ⓒ NAVER Corp.</a></address> </div> </div> </div> </div> <div id="adscript" style="display:none"></div> </body> </html>
3. POST 요청
curl -X POST -d "param1=value1¶m2=value2" https://example.com/api/submit
4. POST 요청 ( json payload )
curl -H "Content-Type: application/json" -X POST -d '{"key1":"value1","key2":"value2"}' https://example.com/api/submit
5. GET 요청 with parameters
curl "https://example.com/api/search?query=value1&sort=value2"
6. 오토 리다이렉트
$ curl localhost:8080
<!doctype html>
<html lang=en>
<title>Redirecting...</title>
<h1>Redirecting...</h1>
<p>You should be redirected automatically to the target URL: <a href="/home">/home</a>. If not, click the link.
- 위 처럼 리다이렉트 되는 주소에 접근하려 할 때,
-L
옵션을 사용하여 리다이렉트 된 응답 데이터를 출력한다.
curl -L localhost:8080
유용한 옵션
1. 타임아웃 지정
curl --connect-timeout 10 https://example.com
2. SSL 인증 오류 무시
curl --insecure https://example.com
3. 콘솔 로그 출력
curl -v https://example.com
- url:port 접속 확인 시 유용하게 사용
4. 인증 사용자 패스워드 입력
curl -u username:password https://example.com/secure
반응형