레이블이 RESTful인 게시물을 표시합니다. 모든 게시물 표시
레이블이 RESTful인 게시물을 표시합니다. 모든 게시물 표시

curl 유용한 사용법 몇가지

curl 유용한 사용법 몇가지
curl은 REST API 방식으로 개발하는 경우 필수적인 요소인듯 싶다.
몇가지 간단한 사용법만 알아보자.

1. GET 방식으로 보내기
curl http://host_address:port

2. 헤더정보 받기.
curl -i http://host_address:port

3. 오직 헤더만 받기.
curl -s    -o /dev/null    -w "%{http_code}\n" http://host_address:port

4. Header 추가해서 요청하기.
curl -H "Content-Type: application/json" http://host_address::port

5. POST 방식으로 보내기
curl -X POST http://host_address:port -d '{"key":"value", "key2":"value2"}'

6. 서버의 파일 저장하기. (내가 원하는 이름으로)
curl -o filename.txt http://host_address:port/fileurl

7. 서버의 파일 다운로드 하기 (서버에 있는 이름 그대로)
curl -O http://host_address:port/fileurl

8. 처리 시간 알아보기.
curl http://host_address:port/url -w "%{time_connect} + %{time_starttransfer} = %{time_total}"

%{time_connect} : 서버 접속시간
%{time_starttransfer} : 파일 전송시간
%{time_total} : 총 처리되어서 전송된 시간 (여기에는 서버 내부에서 실행된 시간도 포함이 된다. )

기타 나머지 : 
> curl --manual 


Available --write-out variables

Some of these variables are not available in really old curl versions.
  • %{content_type} shows the Content-Type of the requested document, if there was any.
  • %{filename_effective} shows the ultimate filename that curl writes out to. This is only meaningful if curl is told to write to a file with the --remote-name or --output option. It's most useful in combination with the --remote-header-name option.
  • %{ftp_entry_path} shows the initial path curl ended up in when logging on to the remote FTP server.
  • %{response_code} shows the numerical response code that was found in the last transfer.
  • %{http_connect} shows the numerical code that was found in the last response (from a proxy) to a curl CONNECT request.
  • %{local_ip} shows the IP address of the local end of the most recently done connection—can be either IPv4 or IPv6
  • %{local_port} shows the local port number of the most recently made connection
  • %{num_connects} shows the number of new connects made in the recent transfer.
  • %{num_redirects} shows the number of redirects that were followed in the request.
  • %{redirect_url} shows the actual URL a redirect would take you to when an HTTP request was made without -L to follow redirects.
  • %{remote_ip} shows the remote IP address of the most recently made connection—can be either IPv4 or IPv6.
  • %{remote_port} shows the remote port number of the most recently made connection.
  • %{size_download} shows the total number of bytes that were downloaded.
  • %{size_header} shows the total number of bytes of the downloaded headers.
  • %{size_request} shows the total number of bytes that were sent in the HTTP request.
  • %{size_upload} shows the total number of bytes that were uploaded.
  • %{speed_download} shows the average download speed that curl measured for the complete download in bytes per second.
  • %{speed_upload} shows the average upload speed that curl measured for the complete upload in bytes per second.
  • %{ssl_verify_result} shows the result of the SSL peer certificate verification that was requested. 0 means the verification was successful.
  • %{time_appconnect} shows the time, in seconds, it took from the start until the SSL/SSH/etc connect/handshake to the remote host was completed.
  • %{time_connect} shows the time, in seconds, it took from the start until the TCP connect to the remote host (or proxy) was completed.
  • %{time_namelookup} shows the time, in seconds, it took from the start until the name resolving was completed.
  • %{time_pretransfer} shows the time, in seconds, it took from the start until the file transfer was just about to begin. This includes all pre-transfer commands and negotiations that are specific to the particular protocol(s) involved.
  • %{time_redirect} shows the time, in seconds, it took for all redirection steps including name lookup, connect, pre-transfer and transfer before the final transaction was started. time_redirect shows the complete execution time for multiple redirections.
  • %{time_starttransfer} shows the time, in seconds, it took from the start until the first byte was just about to be transferred. This includes time_pretransfer and also the time the server needed to calculate the result.
  • %{time_total} shows the total time, in seconds, that the full operation lasted. The time will be displayed with millisecond resolution.
  • %{url_effective} shows the URL that was fetched last. This is particularly meaningful if you have told curl to follow Location: headers (with -L).

Richardson's Maturity Model

Richardson's Maturity Model


- (RMM)은 Leonard Richardson에 의해서 개발된 REST 기반의 웹 서비스 분류법이다.
- 4단계로 분류하고 있다.

Richardson's Maturity Model

- RMM은 서로다른 웹 서비에 대해서 이들간의 디자인, 이점, 균형들에 대해서 이해할 수 있도록 해준다.

Level Zero :
  - 가장 기본적인 성숙 레벨이다.
  - HTTP를 전송 메커니즘으로 사용하고, 원격 프로시저 콜을 하나의 URL을 이용하여 수행한다.
  - 보통 POST와 GET HTTP 메소드를 이용한다.
  - SOAP, XML-RPC 기반의 웹 서비스가 여기에 해당한다.

Level One :
  - 복수개의 URI를 통해서 REST에 더욱 가깝게 다가가는 모델이다.
  - 이는 하나의 리소스에 하나의 URL을 할당한다.
  - 큰 서비스 엔드포인트의 복잡한 기능을 복수개의 리소스로 분리하여 구축한다.
  - 그러나 서비스는 하나의 HTTP Verb를 이용한다. 보통 POST로 모든 처리를 한다.

Level Two :
  - HTTP프로토콜을 이용하고, HTTP verb를 적절하게 사용한다. 그리고 상태 코드를 통해서 적절한 응답을 제공한다.
  - CRUD 서비스를 제공한다.

Level Three :
  - 가장 성숙된 모델이다. 이것은 Hypermedia의 개념을 바탕으로 구축이 된다.
  - 어플리케이션 상태 엔진과 HATEOAS를 이용한다.
  - 이 서비스에서는 컨텐츠 내용을 통해서 관련된 리소스와 컨트롤을 파악할 수 있고, 이에따라 다음 행동을 취할 수 있도록 한다.


HTTP Status Code

HTTP Status Code

Http Status Code 분류 

1. Informational Codes : 
  - 서버가 요청을 받아 들였으나 아직 끝나지 않은 상태를 의미한다. 
  - 100 번대 코드

2. Success Codes : 
  - 요청을 성공적으로 수신했고 처리 했음을 의미한다. 
  - 200 번대 코드

3. Redirection Codes :
  - 요청을 성공적으로 수행했으나 클라이언트가 추가적인 액션을 해야 모든 요청이 끝이남을 의미한다. 
  - 이 액션은 보통 redirecting을 수행하여 다른 리소스를 받아야 하는 경우 사용됨
  - 300 번대 코드

4. Client Error Codes :
  - 클라이언트 요청에 문제가 있을경우를 의미한다. 
  - 400 번대 코드

5. Server Error Codes :
  - 클라이언트 요청을 처리하는데 서버 상에서 오류가 났음을 의미한다. 
  - 500 번대 코드 

현재 사용되는 상태 코드

100 : Continue 
  - 서버가 첫번째 요청의 부분을 수신했음을 나타낸다. 남은 요청이 아직 남아 있을 것임을 의미한다. 

200 : OK
  - 모든 요청이 성공적으로 수행되었다. 

201 : Created
  - 요청이 완료 되었고 새로운 리소스가 생성 되었다. 

202 : Accepted
  - 요청이 받아졌다. 그라나 처리중에 있다. 

204 : No Content
  - 서버가 요청을 모두 처리하였으나, 클라이언트에 보낼 데이터가 없다

301 : Move Permanently
  - 요청된 자원이 새로운 위치로 이동 되었고, 이 자원에 접근하기 위해서는 새로운 URI가 필요하다는 것을 나타낸다. 

400 : Bad Request
  - 클라이언트 요청이 형식에 맞지 않고, 서버는 이 요청을 이해할 수 없다.

401 : Unauthorized
  - 클라이언트는 자원에 접근하기 전에 인증을 받아야 한다. 만약 요청이 이미 클라이언트의 Credential 정보를 포함하고 있다면 401은 유효하지 않는 Credential 정보라는 것을 의미한다. 

403 : Forbidden
  - 서버는 요청을 이해했다. 그러나 이 요청을 거부한다. 
  - 클라이언트가 Blacklist IP에 속하거나, 승인 타임을 벗어난 요청인경우 발생될 수 있다. 

404 : Not Found
  - 요청된 URI에 원하는 리소스가 없다 

406 : Not Acceptable 
  - 서버는 요청을 처리할 수 있다. 그러나 생성된 응답을 클라이언트가 받을 수 없을 경우이다. 
  - 이것은 클라이언트가 헤더를 받을때 너무 엄격하거나 까다로운경우 발생할 수 있다.

500 : Internal Server Error
  - 서버가 요청을 처리하는 도중 에러가 발생하였다. 요청은 완료될 수 없다.

503 : Service Unavailable
  - 요청이 완료될 수 없다. 서버에 부하가 걸렸거나, 점검 상태에 들어간 경우일 수 있다. 
  - 서버 장비가 다운 되었을때도 나타난다. ex) nginx 살아있음, tomcat 죽음 ...