13 Şubat 2020 Perşembe

curl komutu

Giriş
curl ve wget farklı işler için kulanılırlar. Açıklaması şöyle.
wget provides a different set of functions.  wget is the best tool for downloading websites and is capable of recursively traversing directories and links to download entire sites.
For downloading websites, use  wget.
URL'nin  Büyük Küçük Harf Duyarlı Olması
URL adresinin büyük küçük harf duyarlı olması tamamen HTTP sunucusuna bağlı. curl bunu kontrol edemez. Açıklaması şöyle
It's not curl that decides case-insensitivity at all – link "checking" is the server's decision.

(In other words, HTTP clients do not have the opportunity to see the list of all possible URLs and choose a matching one. The only thing an HTTP client can do is give the exact URL to the server and let the server decide how to respond. Some servers are case-insensitive, some are not.)

-a/--append seçeneği
Örnek ver

-A/--user-agent <agent string> seçeneği
Şöyle yaparız.
curl --user <username>:<password> --user-agent "user"  http://...
-C/--continue-at <offset> seçeneği
Açıklaması şöyle. Yarıda kesine indirme işlemine devam edebilmeyi sağlar.
Use -C - to tell curl to automatically find out where/how to resume the transfer. It then uses the given output/input files to figure that out.
Açıklaması şöyle. -C seçeneğinde sonra - karakteri gelmelidir.
The  -C switch is what resumes our file transfer but also notice that there is a dash (-) directly after it. This tells cURL to resume the file transfer, but to first look at the already downloaded portion in order to see the last byte downloaded and determine where to resume.
Örnek
Şöyle yaparız.
curl -s --retry 9999 --retry-delay 3 --speed-limit 2048 --speed-time 10 \
    --retry-max-time 0 -C - -o "${FILENAME}.part${i}" "${URL}" &
-d/--data <data> seçeneği
URL'nin arkasından ? işareti ile gönderilen parametrelerdir. POST için kullanılır.

Örnek
Normalde tarayıcı ile şöyle yaparız.
http://a.b.c.com?PanSingleMoveDegree=5&TiltSingleMoveDegree=5&PanTiltSingleMove=5
Post işlemi için curl ile şöyle yaparız.
curl --data "PanSingleMoveDegree=5&TiltSingleMoveDegree=5&PanTiltSingleMove=5" http://..
Örnek - JSON Post
Json göndermek için şöyle yaparız. Burada -d seçeneğinden sonra veri tek tırnak ile gönderiliyor.
curl -X POST -H "Authorization: Bearer YOUR-TOKEN" -H "Content-Type: application/json"
-d '{
    "message":{
    "token":"TARGET_DEVICE_TOKEN",
    "notification":{
      "title":"Hello",
      "body":"This is a text message!"
    }
  }
}' https://fcm.googleapis.com/v1/projects/YOUR-PROJECT-SHORT-NAME/messages:send
Örnek - JSON Post
Döngü kurmak için şöyle yaparız. Burada tek tırnak içindeki kısım variable expansion'a tabi tutulmaz. Yani '{"user-name":"' kısmı aynen gelir. Daha sonra i değişkenin değeri gelir daha sonra da '", "password": "'  gelir.
for i in john frank bob
do
  curl -X POST  --anyauth -u admin:admin --header "Content-Type:application/json" \
  -d '{"user-name":"'$i'",
       "password": "'$i'",
       "role": [ "rest-reader", "rest-writer" ]
      }' \
  http://localhost:8002/manage/v2/users
done
--data-binary <data> seçeneği
Belirtilen dosyayı olduğu gibi gönderir. SOAP göndermek için şöyle yaparız.
curl -H 'Content-Type: application/soap+xml; charset=UTF-8' \
     -H 'SOAPAction: Some-Action' \
     --data-binary @request.xml \
     <endpoint>
-f/--fail seçeneği
Açıklaması şöyle.
--fail will make the exit status nonzero on a failed request.
Verilen url yoksa 0'dan farklı bir hata kodu ile çıkar. Şöyle yaparız.
if curl --head --fail --silent "$url" >/dev/null; then
    touch .images/"${url##*/}"
fi
-http1.1 seçeneği
Şöyle yaparız
curl -sL --http1.1 https://cnfl.io/cli
-H/--header<header> seçeneği
Header eklemek için kullanılır. Şöyle yaparız.
curl -H "Content-Type: application/json" -X POST  --data  '{"maxRowNum":"500",
"maxColumnNum":10}' http://...
Şöyle yaparız.
$ curl -H "Referer: https://www.google.com/" http://www.foo.com
-I seçeneği/--head seçeneği
Sadece HTTP header'larını getirir.Açıklaması şöyle.
--head will avoid downloading the file contents
Örnek
Bir adresin çalışıp çalışmadığını test etmek için şöyle yaparız.
$ curl -Is "httpbin.org" >/dev/null  2>&1
$ echo $?
0
$ curl -Is "non-existing-domain-lalalala.com" >/dev/null  2>&1
$ echo $?
6
-i/--include seçeneği
Şöyle yaparız.
curl -si http://localhost:8080/1
HTTP/1.1 200
Content-Type: application/json;charset=UTF-8
Transfer-Encoding: chunked

[{"id":1,"name":"name11"}]
-k/--insecure seçeneği - Disable  Server Certificate Verification
Örnek
Şöyle yaparız
curl --insecure 'https://...' 
  -d ssrc=login 
  -d email=Webster 
  -d password=hunter1 
  --header 'Host: meta.stackexchange.com'
-L seçeneği - Follow Redirect
Açıklaması şöyle.
By default,  cURL won't follow the redirect, but you can tell it to with the -L switch.
Örnek
Şöyle yaparız.
curl -L "https://api.github.com/repos/<org>/<repo>/tarball/$commit_sha
?access_token=$github_token" | tar -xz -C "$extract_dir/"
-m seçeneği - timeout
Açıklaması şöyle
You can specify a maximum time to spend executing a command with the -m switch. When the specified time has elapsed,  cURL will exit whatever it's doing, even if it's in the middle of downloading or uploading a file.

cURL expects your maximum time to be specified in seconds.
-O seçeneği
Çıktıyı "remote file name" olarak bulunduğumuz dizine kaydeder.

--output seçeneği
Çıktıyı kaydetmek istediğimiz dosya ismini belirtir.

-s/ --silent seçeneği
Açıklaması şöyle.
--silent will avoid status or errors from being emitted by the check itself.
-T seçeneği
Açıklaması şöyle.
You can use the  -T option if you want to upload a file to an FTP server.
-u/--user seçeneği
Örnek
Şöyle yaparız.
curl --user <username>:<password>  http://...
-x seçeneği - Proxy
Açıklaması şöyle.
It's easy to direct cURL to use a proxy before connecting to a host.  cURL will expect an HTTP proxy by default, unless you specify otherwise.


Use the  -x switch to define a proxy.
-X/--request <command> seçeneği
-X veya --request şeklinde kullanılabilir.

Örnek - DELETE
Şöyle yaparız.
curl -X DELETE "Content-Type: application/json" localhost:8080/api/delete
Örnek - GET
Normalde curl -X GET seçeneğini vermezsek POST gönderir. Elimizde şöyle bir komut olsun. Bu komut POST isteği yapar.
curl -H "Content-Type: application/json" -d "{ \"messageId\": \"275\" }"
  localhost:8080/api/unread
Bunu sadece GET yapmak için şöyle yaparız
curl -H "Content-Type: application/json" \
  -X GET \ 
  -v \ # enable debug output to see response headers
  localhost:8080/api/unread
Örnek - POST
Http PUT kullanarak dosya göndermek için şöyle yaparız.
curl -X PUT --data-binary @/some/local/file.txt -o response.txt 
  https://example.com/file.txt
API
CPR
Post için şöyle yaparız.
#include <cpr/cpr.h>

auto r = cpr::Post(cpr::Url{"http://www.httpbin.org/post"},
                   cpr::Body{"This is raw POST data"},
                   cpr::Header{{"Content-Type", "text/plain"}});
std::cout << r.text << std::endl;


Hiç yorum yok:

Yorum Gönder