====== Curl ====== * https://curl.se/ * https://curl.se/docs/manpage.html ===== 기본옵션 ===== * ''-v'' : 요청과 응답을 상세하게 로그로 찍는다. * ''$HOME/.curlrc'' 파일에 기본 옵션들을 지정해 둘 수 있다. * ''-o [FILENAME]'' 응답 결과를 파일로 저장한다. * ''-s''/''%%--silent%%'' : 본문 외의 다른 메시지는 찍지 말 것. * ''-L'' : redirect 따라가기 * ''-f''/''%%--fail%%'' : fail silently * ''-S'' : ''-s''와 함께 사용시, 에러 발생시 에러보여주기 ===== 순수하게 컨텐츠만 받기 ===== * curl 컨텐츠만 순수하게 받고, 그 내용에 pipeline 을 수행하려면 아래 옵션을 하는게 좋다. curl -fsSL "" ===== 인증 ===== curl --user name:password http://www.example.com ''name:password'' 에 특수 문자가 들어갈 경우 홑따옴표로 감싸면 된다. ===== method ===== ''-X GET|POST|PUT|HEAD|...'' 로 메소드를 지정할 수 있다. ===== POSTing File ===== * [[https://davidwalsh.name/curl-post-file|Curl Post File]] * ''-F'' * ''%%--%%data-binary'' ===== 응답 헤더 출력 ===== ''-D, %%--%%dump-header '' 으로 헤더를 덤프할 수 있다. ''''을 ''-''로 지정하면 스트림으로 출력한다. ===== 헤더 지정 ===== ''-H "HeaderKey: HeaderValue"'' 혹은 ''%%--%%header "HeaderKey: HeaderValue"'' 형태로 요청 헤더를 지정할 수 있다. ===== Shell script 등에서 실패시 exit fail ===== * shell script 등에서 curl 요청의 응답이 ''200'' 이 아닌 경우 shell script를 실패하게 하려면 ''-s -w "%{http_code}"''를 사용한다. response=$(curl -s -w "%{http_code}" -o /path/to/download.zip http://url.to/target.zip) if [ "$response" != "200" ] then exit 1 fi ===== Basic Auth ===== curl --user myusername:mypassword http://... ===== json ===== * [[https://daniel.haxx.se/blog/2022/02/02/curl-dash-dash-json/|curl dash-dash-json | daniel.haxx.se]] * ''7.82'' 버전부터 ''%%--json%%'' 옵션 생김 # --json 은 아래와 동일한 옵션이다. --data [arg] --header "Content-Type: application/json" --header "Accept: application/json" # 기본 사용법 curl --json '{"tool": "curl"}' https://example.com/ # json 파일 지정 curl --json @json.txt https://example.com/ # stdin echo '{"a":"b"}' | curl --json @- https://example.com/ * [[linux:jo|jo]], [[linux:jq|jq - json query]] 등과 함께 사용가능. jo name=daniel tool=curl | curl --json @- https://httpbin.org/post | jq ===== tiemout ===== * ''%%--connect-timeout 3.14%%'' : connection timeout 초. fractional * ''%%--max-time 2.92%%'' : 전송 시간 제한 초. fractional. 아마도 read-timeout 같은 역할인듯보임. ===== retry ===== * [[https://stackoverflow.com/questions/42873285/curl-retry-mechanism|rest - Curl retry mechanism - Stack Overflow]] * ''%%--max-time 10%%'' : 각 retry 가 기다릴 시간 초. * ''%%--retry 5%%'' : 재시도 횟수 최대 5회 * ''%%--retry-delay 3%%'' : 재시도 마다 주어질 delay 초 ===== OpenSSL 인증서 실패 ===== 갑자기 아래와 같은 오류가 발생했는데, 이는 curl이나 운영체제의 문제가 아니라 당시 접속하고 있던 해당 WiFi 망의 라위터가 인증서를 조작한 것으로 보인다. OpenSSL SSL_connect: SSL_ERROR_SYSCALL in connection to raw.githubusercontent.com:443 혹은 http_proxy, https_proxy 같은 환경 변수 설정의 proxy 서버에서 인증서를 변조할 수도 있다. 이로인해, curl 뿐만 아니라 웹 브라우저에서도 올바로 접속이 안됐다. 망이나 프록시 서버를 바꿔서 접속하니까 잘 됐다.