Networking, Security, Protocols

Curl(Client URL)이란?

Somaz 2023. 5. 26. 20:32
728x90
반응형

Overview

오늘은 Curl(Client URL)에 대해서 공부해보려고 한다.

출처 : https://curl.se/

 
HTTP Method를 알아야 이해하기 쉬우니 아래의 포스팅을 참고하길 바란다.
2023.05.25 - [CS 지식] - [CS 지식6.] HTTP 메서드(Method)란? / HTTP Status Code

 

[CS 지식6.] HTTP 메서드(Method)란? / HTTP Status Code

Overview 오늘은 HTTP 메서드에 대해서 공부해보려고 한다. HTTP 메서드(Method)란? HTTP 메서드는 클라이언트가 웹 서버에게 어떤 종류의 동작을 원하는지를 나타내는 방법이다. 각 메서드는 특정한 종

somaz.tistory.com

 


Curl이란?

curl은 서버와 데이터를 주고 받는 데 사용되는 명령줄 도구이다. HTTP, HTTPS, FTP 등 다양한 프로토콜을 지원한다.

curl은 API 테스트, 응답 헤더 보기 및 웹 서버에 요청하는 데 널리 사용된다.

 

Curl 옵션

Option Description
-X, --request Specifies a custom request method to use.
-H, --header Sends custom HTTP headers in the request.
-d, --data Sends the specified data in a POST request to the server.
-I, --head Fetches only the headers from the response.
-v, --verbose Provides more detailed output about the request and response.
-o, --output Saves the output to a file instead of printing it to the console.
-O, --remote-name Saves the output to a file with the same name as on the server.
-u, --user Specifies the username and password to use for server authentication.
-L, --location Follows HTTP 3xx redirects.
-s, --silent Silent mode. Don't output anything.
-k, --insecure Allows connections to SSL sites without certificates.
-A, --user-agent Sends the User-Agent string to the server.
-b, --cookie Sends cookies to the server.
-c, --cookie-jar Saves cookies after the request to a file.
-F, --form Sends data as a multipart form.
-G, --get Sends data in the URL with a GET request.
-T, --upload-file Uploads a file to a specified URL.

Curl 사용법

 

1. Basic usage

웹 페이지의 콘텐츠를 가져오려면 아무 옵션도 주지 않으면 된다. (default 옵션 GET)
기본적으로 제공한 URL로 GET 요청을 보내고 터미널에 출력(일반적으로 HTML)을 한다.

curl https://www.example.com

curl www.google.com
<!doctype html><html itemscope="" itemtype="http://schema.org/WebPage" lang="ko"><head><meta content="text/html; charset=UTF-8" http-equiv="Content-Type"><meta content="/images/branding/go
ogleg/1x/googleg_standard_color_128dp.png" itemprop="image"><title>Google</title><script nonce="lCagotofvDtm4lS3GMQZDw">(function(){var _g={kEI:'WypvZNiKBMXw9APO07XICg',kEXPI:'0,1359409,1709,4349,207,4804,2316,383,246,5,1129120,1703,1196069,119,380600,16114,28684,22431,1361,12319,17580,4998,17075,38444,2872,2891,11754,606,60690,2614,3784,9707,230,20583,4,1528,2304,42126,114
...

 

2. Specifying request method

POST와 같은 다른 HTTP 메서드를 사용하려면 -X 옵션 뒤에 메서드를 사용할 수 있다.

curl -X POST https://www.example.com

 

3. Sending data with a request

POST 요청을 하는 경우 -d 옵션을 사용해서 데이터를 보낼 수 있다.

curl -X POST -d "param1=value1&param2=value2" https://www.example.com

 
-H 옵션을 사용하여 JSON 데이터를 보낼 때 (또는 --header)는 적절한 헤더를 설정하면 된다.

curl -X POST -d '{"key":"value"}' -H "Content-Type: application/json" https://www.example.com

 

4. Saving output to a file

-o는 미리 정의된 파일 이름을 사용하여 파일을 저장할 수 있다.

curl -o example.html https://www.example.com

curl -o somaz.zip https://releases.hashicorp.com/terraform-provider-aws/4.8.0/terraform-provider-aws_4.8.0.0_darwin_arm64.zip
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100   347    0   347    0     0    614      0 --:--:-- --:--:-- --:--:--   614

ls
somaz.zip

 
 
-O는 파일을 원래 파일 이름으로 저장한다.

curl -O https://www.example.com

curl -O https://releases.hashicorp.com/terraform-provider-aws/4.8.0/terraform-provider-aws_4.8.0.0_darwin_arm64.zip
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100   347    0   347    0     0    624      0 --:--:-- --:--:-- --:--:--   624

ls
somaz.zip terraform-provider-aws_4.8.0.0_darwin_arm64.zip

 

5. Following redirects

리디렉션(HTTP 3xx 응답)을 따르려면 ( -L또는 --location) 옵션을 사용한다.
curl로 파일을 다운받을때 -O 옵션과 함께 많이 사용한다.

curl -LO https://www.example.com

 

6. Verbose mode

요청 및 응답에 대한 자세한 정보를 보려면 -v(또는 --verbose) 옵션을 사용한다.

curl -v https://www.example.com

 

7.  Use  API(Github Action PR)

gitlab CI 에서 아래의 API를 통해 PR을 날릴 수 있다.

curl -s -X POST -H "Authorization: token $GITHUB_TOKEN" -H "Accept: application/vnd.github+json" https://api.github.com/repos/${{ github.repository }}/pulls -d '{"title": "Add test.txt file", "head": "test1", "base": "main"}'

 

8. Ignore Certificate Validation

curl의 -k 또는 --insecure 옵션은 인증서 유효성 검사를 무시하도록 curl에 지시하는 데 사용된다. 그리고 자체 서명된 SSL 인증서가 있는 사이트에 연결하거나 SSL 인증서 확인을 우회하려는 경우에 주로 사용된다.

curl -k https://self-signed.badssl.com/

 

9. Interesting Function

 
공인 IP 확인하기

curl -s ipinfo.io/ip
14.xx.xx.xxx

curl ifconfig.me
14.xx.xx.xxx

 
날씨 확인하기

 curl http://wttr.in/Seoul
Weather report: Seoul

      \   /     Sunny
       .-.      18 °C
    ― (   ) ―   ↖ 7 km/h
       `-’      10 km
      /   \     0.0 mm
                                                       ┌─────────────┐
┌──────────────────────────────┬───────────────────────┤  Fri 26 May ├───────────────────────┬──────────────────────────────┐
│            Morning           │             Noon      └──────┬──────┘     Evening           │             Night            │
├──────────────────────────────┼──────────────────────────────┼──────────────────────────────┼──────────────────────────────┤
│     \   /     Sunny          │     \   /     Sunny          │               Cloudy         │               Overcast       │
│      .-.      21 °C          │      .-.      26 °C          │      .--.     +24(25) °C     │      .--.     21 °C          │
│   ― (   ) ―   ↑ 5-6 km/h     │   ― (   ) ―   ↗ 11-13 km/h   │   .-(    ).   → 13-14 km/h   │   .-(    ).   → 6-8 km/h     │
│      `-’      10 km          │      `-’      10 km          │  (___.__)__)  10 km          │  (___.__)__)  10 km          │
│     /   \     0.0 mm | 0%    │     /   \     0.0 mm | 0%    │               0.0 mm | 0%    │               0.0 mm | 0%    │
└──────────────────────────────┴──────────────────────────────┴──────────────────────────────┴──────────────────────────────┘
                                                       ┌─────────────┐
┌──────────────────────────────┬───────────────────────┤  Sat 27 May ├───────────────────────┬──────────────────────────────┐
│            Morning           │             Noon      └──────┬──────┘     Evening           │             Night            │
├──────────────────────────────┼──────────────────────────────┼──────────────────────────────┼──────────────────────────────┤
│     \   /     Sunny          │    \  /       Partly cloudy  │    \  /       Partly cloudy  │    \  /       Partly cloudy  │
│      .-.      +23(25) °C     │  _ /"".-.     +29(28) °C     │  _ /"".-.     +23(25) °C     │  _ /"".-.     21 °C          │
│   ― (   ) ―   ← 6 km/h       │    \_(   ).   ↑ 7-8 km/h     │    \_(   ).   → 16-18 km/h   │    \_(   ).   ↙ 2-3 km/h     │
│      `-’      10 km          │    /(___(__)  10 km          │    /(___(__)  10 km          │    /(___(__)  10 km          │
│     /   \     0.0 mm | 0%    │               0.0 mm | 0%    │               0.0 mm | 0%    │               0.0 mm | 0%    │
└──────────────────────────────┴──────────────────────────────┴──────────────────────────────┴──────────────────────────────┘
                                                       ┌─────────────┐
┌──────────────────────────────┬───────────────────────┤  Sun 28 May ├───────────────────────┬──────────────────────────────┐
│            Morning           │             Noon      └──────┬──────┘     Evening           │             Night            │
├──────────────────────────────┼──────────────────────────────┼──────────────────────────────┼──────────────────────────────┤
│               Overcast       │  _`/"".-.     Light rain sho…│      .-.      Heavy rain     │      .-.      Heavy rain     │
│      .--.     21 °C          │   ,\_(   ).   21 °C          │     (   ).    20 °C          │     (   ).    19 °C          │
│   .-(    ).   ↖ 8-12 km/h    │    /(___(__)  ↖ 7-9 km/h     │    (___(__)   ↗ 9-11 km/h    │    (___(__)   ↗ 9-12 km/h    │
│  (___.__)__)  10 km          │      ‘ ‘ ‘ ‘  10 km          │   ‚‘‚‘‚‘‚‘    5 km           │   ‚‘‚‘‚‘‚‘    5 km           │
│               0.0 mm | 0%    │     ‘ ‘ ‘ ‘   0.5 mm | 89%   │   ‚’‚’‚’‚’    27.0 mm | 89%  │   ‚’‚’‚’‚’    34.1 mm | 68%  │
└──────────────────────────────┴──────────────────────────────┴──────────────────────────────┴──────────────────────────────┘
Location: 서울특별시, 대한민국 [37.5666791,126.9782914]

Follow @igor_chubin for wttr.in updates

 
 
 


Reference

https://ko.m.wikipedia.org/wiki/%ED%8C%8C%EC%9D%BC:Curl-logo.svg
 
https://inpa.tistory.com/entry/LINUX-%F0%9F%93%9A-CURL-%EB%AA%85%EB%A0%B9%EC%96%B4-%EC%82%AC%EC%9A%A9%EB%B2%95-%EB%8B%A4%EC%96%91%ED%95%9C-%EC%98%88%EC%A0%9C%EB%A1%9C-%EC%A0%95%EB%A6%AC#thankYou
 
https://curl.se/
 
 
 

728x90
반응형

'Networking, Security, Protocols' 카테고리의 다른 글

Cilium이란?  (2) 2024.02.06
Vault 설치와 사용법  (2) 2024.01.24
SSID(Service Set Identifier)란?  (0) 2023.04.05
CDN이란?  (0) 2022.11.11
Vault란?  (0) 2022.10.11