SRE/Linux Basics

curl

게임이 더 좋아 2023. 1. 21. 12:25
반응형
728x170

정의

Client for URLs 의 약자

 

 

curl 용도

  1. Retrieve the contents of a URL → 해당 URL에 대한 컨텐츠 탐색 가능
  2. Submit data to a server → POST와 같은 요청으로 data 전송 가능 (Request)
  3. Upload files to a server → PUT과 같은 요청으로 파일 업로드 가능 (Request)
  4. Download files from a server → GET과 같은 요청으로 파일 다운로드 가능 (wget 으로도 다운로드 받음)
  5. Retrieve headers of a URL → 헤더의 정보도 탐색 가능
  6. Retrieve the status code of a URL → Status Code도 탐색 가능
  7. HTTP Authentication → 인증 가능
  8. Follow redirects → 리다이렉트 가능
  9. Support for various protocols →  여러가지 프로토콜 지원 HTTP, HTTPS, FTP, SFTP, SCP, TFTP, TELNET, DICT, LDAP, LDAPS, FILE, IMAP, SMTP, POP3, RTSP, RTMP and more
  10. Support for various options to configure →  request headers, proxy, SSL, and more.
  11. Support for HTTP/2 and QUIC

 

 

 

 

 

 

 

 

curl download vs wget download

 

wget

  • 원래 download를 하기 위한 목적으로 만들어진 것
  • built-in 으로 supported 됨
  • much simpler
  • background 로 donwload함 ↔ curl은 기본적으로는 제공하지 않음
  • Linux , Unix-based OS에서만 지원됨

 

curl

  • 더 다양한 목적으로 만들어짐
  • built-in으로 지원하지 않음
  • much complexer
  • background downloading은 추가가 필요함
  • 조금 더 다양한 프로토콜이 사용 가능함
  • 다른 Linux, Unix 이외의 OS에서도 지원함

 

 

 

Curl HTTP Request

 

curl POST

 
#기본 포맷
curl -X POST [URL] -d [data]

#직접 데이터를 넣어도 됨
curl -X POST -H "Content-Type: application/json" -d '{"key1":"value1","key2":"value2"}' https://example.com/submit

#파일도 전송가능
curl -X POST -H "Content-Type: application/json" -d @data.json https://example.com/submit

 

 

curl GET

 
#기본 포맷
curl [URL]

# 응용
curl -H "Authorization: Bearer TOKEN" -H "User-Agent: MyApp" https://example.com

 

curl PUT

 
#기본 포맷
curl -X PUT [URL] -d [data]

# 응용
curl -X PUT https://example.com/upload -T file.txt

curl -X PUT https://example.com/upload --data-binary "@file.txt"

curl -X PUT https://example.com/upload --data-binary "@file.txt" -H "Authorization: Bearer TOKEN" -H "User-Agent: MyApp"

 

 

curl DELETE

 
#기본 포맷
curl -X DELETE [URL]

#응용
curl -X DELETE https://example.com/delete

curl -X DELETE https://example.com/delete -H "Authorization: Bearer TOKEN" -H "User-Agent: MyApp"

 

 

Options

 


 

Option
Description
-X, --request Specify the request method (GET, POST, PUT, DELETE, etc.). Default is GET.
-d, --data Specify data to send in the request body.
--data-urlencode Send data in the request body, but with key-value pairs encoded.
-G, --get Send the data in the request body as a GET request.
-H, --header Add a custom header to the request.
-I, --head Retrieve only the headers of the response.
-o, --output Save the response to a file.
-s, --silent Suppress progress output.
-w, --write-out Specify what information to display after the request is completed.
--compressed Request compressed response.
--location Follow redirects.
--max-time Maximum time allowed for the request.
--user-agent Specify a custom user agent.

 

 

Debug Options ★


 

Option Description
-v Enables verbose mode, which will show detailed information about the request and response, including headers, and the response body
-i Includes the headers in the response
-w Specifies the format of the output, it is possible to use variables like the http code, total time, and size of the download
-D Saves the headers to a file
--trace-ascii Writes the trace to a file in ascii format
--trace-time Include timestamp on each trace line
--trace-redirect Show redirections in the trace
--trace-out Writes the trace to a specified file
--verbose this option is similar to -v

 

 

 

 

Header Field

 


 

Header Description
Accept Indicates the media types that are acceptable for the response.
Accept-Charset Indicates the character encodings that are acceptable for the response.
Accept-Encoding Indicates the types of content encoding that are acceptable for the response.
Accept-Language Indicates the preferred language for the response.
Authorization Used to provide authentication credentials for the request.
Cache-Control Specifies the caching behavior for the response.
Connection Specifies the type of connection to be used.
Content-Encoding Indicates the type of content encoding used for the request or response.
Content-Length Indicates the size of the request or response body.
Content-Type Indicates the type of data in the request or response body.
Cookie Used to send information about the client's cookies.
Host Indicates the host and port number of the requested resource.
If-Modified-Since Specifies a date after which the response should be considered stale.
If-None-Match Specifies an ETag value for the requested resource, for use in conditional requests.
Referer Indicates the URL of the page that linked to the requested resource.
User-Agent Specifies information about the client software and device.

 

 
#위의 모든 헤더 필드를 포함한 요청

curl -X GET "https://example.com" -H "Accept: application/json" -H "Accept-Charset: utf-8" -H "Accept-Encoding: gzip, deflate" -H "Accept-Language: en-US" -H "Authorization: Bearer YOUR_TOKEN" -H "Cache-Control: no-cache" -H "Connection: keep-alive" -H "Content-Type: application/json" -H "Host: example.com" -H "Referer: https://www.example.com" -H "User-Agent: MyApp/1.0"

 

 

Body Field


 

Body Type Description
JSON JavaScript Object Notation is a lightweight data-interchange format that is easy for humans to read and write and easy for machines to parse and generate. JSON is often used when data is sent from a server to a web page.
XML Extensible Markup Language is a markup language that is used to store and transport data. XML is often used when data is sent from a server to a web page.
Form data Form data is used to send data in the request body as key-value pairs, typically as the result of a HTML form submission.
Multipart/form-data Multipart/form-data is used when sending binary data in the request body, such as a file upload.
Binary data Binary data is any type of data that is not in a human-readable format, such as a PDF or image file.

 

각 타입 별로 예

JSON

curl -X POST "https://example.com" -H "Content-Type: application/json" -d '{"name":"John","age":30}'

XML

curl -X POST "https://example.com" -H "Content-Type: application/xml" -d "<person><name>John</name><age>30</age></person>"

Form Data

curl -X POST "https://example.com" -H "Content-Type: application/x-www-form-urlencoded" -d "name=John&age=30"

Multipart/form-data

curl -X POST "https://example.com" -H "Content-Type: multipart/form-data" -F "file=@/path/to/file.txt" -F "name=John" -F "age=30"

Binary data

curl -X POST "https://example.com" -H "Content-Type: application/octet-stream" --data-binary "@/path/to/file.jpg"
반응형
그리드형

'SRE > Linux Basics' 카테고리의 다른 글

디렉토리 역할 - /bin  (1) 2023.12.22
Linux Default File System  (0) 2023.08.13
Symblic Link, Hard Link  (0) 2023.01.04
Register Service, 서비스 등  (0) 2023.01.04
touch  (0) 2022.12.31