Curl: Difference between revisions

From NovaOrdis Knowledge Base
Jump to navigation Jump to search
 
(31 intermediate revisions by the same user not shown)
Line 4: Line 4:
* [[wget]]
* [[wget]]
* [[HTTPie]]
* [[HTTPie]]
* [[Bash Handling Complex Commands as Strings#Overview|Handling Complex Commands as Strings in bash]]
=Overview=
If the server responds with 404, curl displays the error message, is any, and exits with 0.
=Installation=
==With apt==
  apt-get update
  apt-get install curl
=Exit Code=
{{External|https://ec.haxx.se/usingcurl/usingcurl-returns}}


=Command Line Configuration=
=Command Line Configuration=
Line 25: Line 39:
  -H "Content-Type: application/json"
  -H "Content-Type: application/json"


==-u==
==<span id='-u'></span>-u, --user==


Authentication:
Authentication:


  curl -u username:password
  curl -u username:password
If no password is specified after ":", curl will challenge for password.


===--basic===
===--basic===


Basic Authentication
Basic Authentication
====Basic Authentication when Having the Basic Authorization Token====
The basic authorization token can be used directly, as shown:
<syntaxhighlight lang='bash'>
curl -H "Authorization: Basic c[...]4"  https://jenkins.example.com/cloud/job/example/job/somejob/33/consoleText > ./build-33.txt
</syntaxhighlight>


===--digest===
===--digest===
Line 65: Line 88:
==-d <data>==
==-d <data>==


Sends the specified data in a POST request to the HTTP server, in the same way that a browser does when a user has filled in an HTML form and presses the submit button. This will cause curl to pass the data to the server using the content-type application/x-www-form-urlencoded.
Sends the specified data in a POST or PUT request to the HTTP server, in the same way that a browser does when a user has filled in an HTML form and presses the submit button. This will cause curl to pass the data to the server using the content-type application/x-www-form-urlencoded.


If the option is used  more than once on the same command line, the data pieces specified will be merged together with a separating &-symbol. Thus, using "-d name=alice -d qualification=coder" would generate a post chunk that looks like 'name=alice&qualification=coder'.
If the option is used  more than once on the same command line, the data pieces specified will be merged together with a separating &-symbol. Thus, using "-d name=alice -d qualification=coder" would generate a post chunk that looks like 'name=alice&qualification=coder'.
Line 79: Line 102:
If what follows after --data-binary starts with @, it is considered to be a name of a file, and the content of the file is sent as data.
If what follows after --data-binary starts with @, it is considered to be a name of a file, and the content of the file is sent as data.


curl ... --data-binary @./some-file.json ...
<syntaxhighlight lang='bash'>
curl ... --data-binary @./some-file.json ...
</syntaxhighlight>
 
<syntaxhighlight lang='bash'>
echo "some_metric 1.0"  | curl --data-binary @- http://localhost:9091/metrics/
</syntaxhighlight>


==-I==
==-I==
Line 88: Line 117:


If the server reports that the requested page has moved to a different location (indicated with a Location: header and a 3XX response code), this option will make curl redo the request on the new place.
If the server reports that the requested page has moved to a different location (indicated with a Location: header and a 3XX response code), this option will make curl redo the request on the new place.
==<tt>-F</tt>, <tt>--form</tt>==
Specify HTTP multipart POST data. See [[#Upload_a_file_with_POST|Upload a file with POST]].
==<tt>-T</tt>, <tt>--upload-file</tt>==
Transfer the specified local file to destination
==<tt>-O</tt>, <tt>--remote-name </tt>==
Write output to a file named as the remote file.
==<tt>-o</tt>, <tt>--output <file> </tt>==
Write to file instead of stdout:
<syntaxhighlight lang='bash'>
curl -o /tmp/somefile.txt https://......
</syntaxhighlight>
==<tt>-n</tt>, <tt>--netrc</tt>==
See [[#.netrc|.netrc]] below.


=Secure Connection=
=Secure Connection=


<font size=-1>
  curl https&#58;//logging-es:9200/ --capath /etc/kibana/keys --key /etc/kibana/keys/key
  curl https&#58;//logging-es:9200/ --capath /etc/kibana/keys --key /etc/kibana/keys/key
</font>


=Download a file and write it locally=
=Download a file and write it locally=


-O instructs curl to write output to a local file name like the remote file we get. Only the file part of the remote file is used. The file will be saved in the current directory.
-O instructs curl to write output to a local file name like the remote file we get. Only the file part of the remote file is used. The file will be saved in the current directory. If you want the file saved in a different directory, make sure you change the current working  directory  before  invoking curl with this option.


  curl -O http&#58;//ftp.gnu.org/gnu/wget/wget-1.15.tar.gz
  curl -O http&#58;//ftp.gnu.org/gnu/wget/wget-1.15.tar.gz
=Upload a file with POST=
<font size=-1>
curl -X POST -F 'file=@./something.tgz'
</font>


=Send REST invocations=
=Send REST invocations=
Line 109: Line 168:
==POST==
==POST==


<pre>
<syntaxhighlight lang='bash'>
curl -H "Content-Type: application/json" -X POST \
curl -H "Content-Type: application/json" -X POST \
   -d '{"id":"1","name":"MyProject","team":"A","language":"java","size":"small"}' \
   -d '{"id":"1","name":"MyProject","team":"A","language":"java","size":"small"}' \
   http://localhost:8080/novaordis-paas/project/MyProject
   http://localhost:8080/novaordis-paas/project/MyProject
</pre>
</syntaxhighlight>
<syntaxhighlight lang='bash'>
echo "some_metric 1.0"  | curl --data-binary @- http://localhost:9091/metrics/
</syntaxhighlight>
Also see [[#Upload_a_file_with_POST|Upload a file with POST]].


==PUT==
==PUT==
Line 144: Line 207:


To use curl with [[Charles]], see: {{Internal|Charles#Using_curl_with_Charles|Using curl with Charles}}
To use curl with [[Charles]], see: {{Internal|Charles#Using_curl_with_Charles|Using curl with Charles}}
=curl and Kubernetes=
{{Internal|Kubernetes and curl|curl and Kuberenetes}}
=<tt>.netrc</tt>=
{{Internal|.netrc#Overview|<tt>.netrc</tt>}}
=Executing curl from Scripts=
{{Internal|Bash Handling Complex Commands as Strings|Handling Complex Commands as Strings in bash}}

Latest revision as of 20:10, 16 October 2023

Internal

Overview

If the server responds with 404, curl displays the error message, is any, and exits with 0.

Installation

With apt

 apt-get update
 apt-get install curl

Exit Code

https://ec.haxx.se/usingcurl/usingcurl-returns

Command Line Configuration

-X

The HTTP method.

-H

Specifies a request header. Multiple headers can be specified. Example:

curl -H "Authentication Bearer: <token>" -H "Content-Type: application/json" ...

Quiet Mode

-s|--silent

Content-Type

-H "Content-Type: application/json"

-u, --user

Authentication:

curl -u username:password

If no password is specified after ":", curl will challenge for password.

--basic

Basic Authentication

Basic Authentication when Having the Basic Authorization Token

The basic authorization token can be used directly, as shown:

curl -H "Authorization: Basic c[...]4"  https://jenkins.example.com/cloud/job/example/job/somejob/33/consoleText > ./build-33.txt

--digest

Digest Authentication

-k --insecure

Insecure.

-v

Verbose. Shows more details about the request and response. A line starting with '>' means "header data" sent by curl, '<' means "header data" received by curl that is hidden in normal cases, and a line starting with '*' means additional info provided by curl.

-s, --silent

Turns on silent or quiet mode. In silent mode curl does not show progress meter or error messages.

-S, --show-error

Like silent more, it disables progress meter, but shows error messages.

-i

Show the HTTP headers in the output.

-A

Specify the User-Agent string to send to the HTTP server. To encode blanks in the string, surround the string with single quote marks. This can also be set with the -H header option. If this option is used several times, the last one will be used.

-d

Sends the specified data in a POST or PUT request to the HTTP server, in the same way that a browser does when a user has filled in an HTML form and presses the submit button. This will cause curl to pass the data to the server using the content-type application/x-www-form-urlencoded.

If the option is used more than once on the same command line, the data pieces specified will be merged together with a separating &-symbol. Thus, using "-d name=alice -d qualification=coder" would generate a post chunk that looks like 'name=alice&qualification=coder'.

If data starts with the letter @, the rest should be a file name to read the data from, or - if you want curl to read the data from stdin. Multiple files can also be specified. When --data is told to read from a file like that, carriage returns and newlines will be stripped out. If you don't want the @ character to have a special interpretation use --data-raw instead.

-d and GET

If -d is used with GET, the data is sent as a entity body.

--data-binary

If what follows after --data-binary starts with @, it is considered to be a name of a file, and the content of the file is sent as data.

curl ... --data-binary @./some-file.json ...
echo "some_metric 1.0"  | curl --data-binary @- http://localhost:9091/metrics/

-I

HEAD only.

-L, --location

If the server reports that the requested page has moved to a different location (indicated with a Location: header and a 3XX response code), this option will make curl redo the request on the new place.

-F, --form

Specify HTTP multipart POST data. See Upload a file with POST.

-T, --upload-file

Transfer the specified local file to destination

-O, --remote-name

Write output to a file named as the remote file.

-o, --output <file>

Write to file instead of stdout:

curl -o /tmp/somefile.txt https://......

-n, --netrc

See .netrc below.

Secure Connection

curl https://logging-es:9200/ --capath /etc/kibana/keys --key /etc/kibana/keys/key

Download a file and write it locally

-O instructs curl to write output to a local file name like the remote file we get. Only the file part of the remote file is used. The file will be saved in the current directory. If you want the file saved in a different directory, make sure you change the current working directory before invoking curl with this option.

curl -O http://ftp.gnu.org/gnu/wget/wget-1.15.tar.gz

Upload a file with POST

curl -X POST -F 'file=@./something.tgz'

Send REST invocations

GET

curl -X GET http://localhost:8080/novaordis-paas/project/

POST

curl -H "Content-Type: application/json" -X POST \
  -d '{"id":"1","name":"MyProject","team":"A","language":"java","size":"small"}' \
  http://localhost:8080/novaordis-paas/project/MyProject
echo "some_metric 1.0"  | curl --data-binary @- http://localhost:9091/metrics/

Also see Upload a file with POST.

PUT

curl -X PUT -d arg=val -d arg=val http://localhost:8080/novaordis-paas/project/MyProject

DELETE

curl -X DELETE http://localhost:8080/novaordis-paas/project/MyProject

Simulation of a HTTP Session JSessionID

curl -b "JSESSIONID=e1VBIIBFhyjU2PLU0JFNwqYu" http://localhost:8080/session-servlet/

Parallel Instances

i=0; while [ $i -lt 20 ]; do (curl http://localhost/&); ((i++)); done

Use curl with a Proxy

It is sometimes useful to configure curl to send request via a proxy. This is achieved with

curl -x http://<proxy-host>:<proxy-port> 

To use curl with Charles, see:

Using curl with Charles

curl and Kubernetes

curl and Kuberenetes

.netrc

.netrc

Executing curl from Scripts

Handling Complex Commands as Strings in bash