Curl: Difference between revisions

From NovaOrdis Knowledge Base
Jump to navigation Jump to search
 
(7 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=
=Overview=
Line 87: 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 136: Line 137:
curl -o /tmp/somefile.txt https://......
curl -o /tmp/somefile.txt https://......
</syntaxhighlight>
</syntaxhighlight>
==<tt>-n</tt>, <tt>--netrc</tt>==
See [[#.netrc|.netrc]] below.


=Secure Connection=
=Secure Connection=
Line 210: Line 213:


=<tt>.netrc</tt>=
=<tt>.netrc</tt>=
 
{{Internal|.netrc#Overview|<tt>.netrc</tt>}}
{{External|https://everything.curl.dev/usingcurl/netrc}}
=Executing curl from Scripts=
 
{{Internal|Bash Handling Complex Commands as Strings|Handling Complex Commands as Strings in bash}}
Unix systems have for a long time offered a way for users to store their user name and password for remote FTP servers, in the form of the <code>.netrc</code> file. The <code>.netrc</code> file is typically stored in a user's home directory. Curl supports it. Format example:
 
<syntaxhighlight lang='text'>
machine example.com
login some-user
password some-password
</syntaxhighlight>

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