Link Search Menu Expand Document (external link)

BE 1.10 cURL

Table of contents


Vad är cURL?

cURL är ett open-source command line verktyg och ett bibliotek för att överföra data med URL:er (det står för client URL). cURL stödjer en mängd olika protokoll och certifikat. Det används i command lines eller script för att överföra data, men också i bilar, tv-apparater, skrivare, mobiltelefoner etc. 1

cURL är alltså namnet på själva open-source projektet, och består av de två produkterna libcurl och curl, där libcurl är ett transfer bibliotek och curl är själva command line verktyget. 2


Vad används cURL till?

  • Hämta content från till exempel FTP och HTTP
  • Skicka HTTP-requests
  • Använda cookies

cURL är kompatibelt med i princip alla operativsystem och anslutna enheter. Det är väldigt smidigt att använda för att testa endpoints i ett API. Det är väldigt mångsidigt och du kan få ut exakt vad som har skickats och mottagits (bra för debugging). 3


curl command options

Det finns över 200 olika options i curl, du kan se alla genom att skriva curl -h i terminalen. Nedan följer några exempel på de vanligaste. 3

-I

Returnerar endast HTTP headers, som content-type, datum etc.

curl --request GET 'https://api.nasa.gov/planetary/apod?api_key=<myapikey>&date=2020-01-01' -I

Exempel svar

HTTP/1.1 200 OK
Date: Tue, 24 May 2022 10:56:37 GMT
Content-Type: application/json
Content-Length: 1512
Transfer-Encoding: chunked
Connection: keep-alive

-v

v = verbose, mångsidigt kommando som returnerar allt som händer när du kör cURL-kommandot, inklusive uppkoppling, headers och returnerad data.

curl --request GET 'https://api.nasa.gov/planetary/apod?api_key=$NASA_API_KEY&date=2020-01-01' -v

Exempel svar

{"date":"2020-01-01",
"explanation":"Why is Betelgeuse fading? No one knows. Betelgeuse, one of the brightest and mo st recognized stars in the night sky, is only half as bright as it used to be only five months ago. Such variability is likely just normal behavior for this famously variable supergiant, but the recent dimming has rekindled discussi on on how long it may be before Betelgeuse does go supernova. Known for its red color, Betelgeuse is one of the few stars to be resolved by modern telescopes, although only barely. The featured artist's illustration imagines how Bet elgeuse might look up close. Betelgeuse is thought to have a complex and tumultuous surface that frequently throws im pressive flares. Were it to replace the Sun (not recommended), its surface would extend out near the orbit of Jupite r, while gas plumes would bubble out past Neptune. Since Betelgeuse is about 700 light years away, its eventual supe rnova will not endanger life on Earth even though its brightness may rival that of a full Moon. Astronomers -- both amateur and professional -- will surely continue to monitor Betelgeuse as this new decade unfolds. Free Presentati on: APOD Editor to show best astronomy images of 2019 -- and the decade -- in NYC on January 3",
"hdur1":"https://apod .nasa.gov/apod/image/2001/BetelgeuseImagined_EsoCalcada_2662.jpg",
"media_type":"image",
"service_version":"v1",
"title" :"Betelgeuse Imagined",
"ur1":"https://apod.nasa.gov/apod/image/2001/BetelgeuseImagined_EsoCalcada_960.jpg"} 

-o

o = output, sparar outputen i en fil.

curl --request GET 'https://api.nasa.gov/planetary/apod?api_key=$NASA_API_KEY&date=2020-01-01' --output curloutput

Referenser