OS/Server > 명령어

curl Linux의 http접속 (GET/POST/JSON) https(ssl), post 방식, 멀티 파일업로드 등등

#centos,#https,#command,#shell,#명령어#multipart#form-data#upload

설치

# apt update && apt install curl  // 우분투
# yum install curl  // CentOS

※ 윈도우는 다운로드 받아 설치
  여기 클릭 !! https://curl.se/windows/

매뉴얼보기

# curl --help
# curl --help all

curl 호출방법

기본옵션들
약옵션 풀옵션 설명
-d --data post방식에서 파라메터설정
-d "param=value&param2=value2"
-H --header -H "Content-Type: application/x-www-form-urlencoded"
-X --request -X POST https://emunhi.com/somedata
https의 경우 -k 옵션추가해 인증무시
-m --max-time --max-time 10
10은 10초로 단위는 second로 명령어를 수행하는데 걸리는 총 시간
none --connect-timeout --connect-timeout 10
10초 : 컨넥션시도 후 응답이 올때까지의 시간
GET 방식

curl -d "key1=value1&key2=value2" \
-H "Content-Type: application/x-www-form-urlencoded" \
-X GET http://192.168.100.100:8080/service/api

POST 방식

curl -d "key1=value1&key2=value2" \
-H "Content-Type: application/x-www-form-urlencoded" \
-X POST http://192.168.100.100:8080/service/api

 

POST-JSON 의경우

# curl --header "Content-Type: application/json" \
 --request POST \
 --data '{"mykey":"myvalue","key2":1234567800}' \
 http://192.168.100.100:8080/service/api/getUser

SSL certificate problem : 인증서 오류를 무시하는 방법

--insecure  또는 -k  옵션을 사용한다.

curl -k https://mysite.com

curl 로 파일업로드 하기

ⓐ 단일파일보내기 (name=file, value='mytest.txt)

    curl -F 'file=@./mytest.txt' https://mysite.com

ⓑ 배열형식으로 여러개 보내기
    curl -F 'file=@./mytest01.txt' -F 'file=@./mytest01.txt' https://mysite.com

ⓒ 파일명을 별도로 지정하고 싶을경우
    curl -F 'file=@./mytest.txt;filename=changedName.txt' https://mysite.com

파일다운로드 하기

curl -O -L http://test.com/file/test.jpg

-L : 리다이렉션을 따라서 ,  -O : url에 있는 이름을 그대로 사용해서 다운로드