Curl: Difference between revisions

From 탱이의 잡동사니
Jump to navigation Jump to search
Line 85: Line 85:


== Examples ==
== Examples ==
'''ssl certification no check'''
=== ssl certification no check ===
<source lang=bash>
<source lang=bash>
$ curl -k -v https://web_sites.com
$ curl -k -v https://web_sites.com
</source>
</source>


'''POST with data'''
=== POST with data ===
<source lang=bash>
<source lang=bash>
$ curl 127.0.0.1:8081 -X POST -d 'test data 123'
$ curl 127.0.0.1:8081 -X POST -d 'test data 123'
</source>
</source>


'''POST with binary data'''
=== POST with binary data ===
<source lang=bash>
<source lang=bash>
$ curl -k -X POST -H "Content-Type: application/json" \
$ curl -k -X POST -H "Content-Type: application/json" \
--data-binary '{ "message": {"username":"test username","password":"4ff5ff499cf340f13d2c4c95859712da"} }' https://pchero21.com:8080/auth
--data-binary '{ "message": {"username":"test username","password":"4ff5ff499cf340f13d2c4c95859712da"} }' https://pchero21.com:8080/auth
</source>
</source>
=== Shows headers ===
<pre>
$ curl -I localhost:8080/login
HTTP/1.1 200 OK
Set-Cookie: cookie-name=MTU1MjQ2NDIwMHxEdi1CQkFFQ180SUFBUkFCRUFBQUpmLUNBQUVHYzNSeWFXNW5EQThBRFdGMWRHaGxiblJwWTJGMFpXUUVZbTl2YkFJQ0FBRT185g9ZNawBhaQfkJPy9RW4fz6mFuZMRT-a8t59QneoCvw=; Path=/; Expires=Fri, 12 Apr 2019 08:03:20 GMT; Max-Age=2592000
Date: Wed, 13 Mar 2019 08:03:20 GMT
</pre>


[[category:command/utility]]
[[category:command/utility]]

Revision as of 08:07, 13 March 2019

Overview

Linux command curl 사용법 정리

Options

-d, --data <data>

Request 전송할 때, 데이터도 같이 전송한다.

(HTTP)  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.   Compare  to
-F, --form.

-d,  --data is the same as --data-ascii. To post data purely binary, you should instead use the --data-binary option. To URL-encode the value of a form
field you may use --data-urlencode.

If any of these options 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=daniel -d skill=lousy' would generate a post chunk that looks like 'name=daniel&skill=lousy'.

If  you start the data 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. Mul‐
tiple files can also be specified. Posting data from a file named 'foobar' would thus be done with --data @foobar. When --data is told to read  from  a
file like that, carriage returns and newlines will be stripped out.

-k, --insecure

SSL 인증 확인을 하지 않는다. 예를 들어, https 통신을 할 때, 상대방 SSL 인증서를 검증하지 않는다.

-u, --user <user:password;options>

user name, password 를 지정한다.

Specify the user name, password and optional login options to use for server authentication. Overrides -n, --netrc and --netrc-optional.

If you simply specify the user name, with or without the login options, curl will prompt for a password.

If  you  use an SSPI-enabled curl binary and perform NTLM authentication, you can force curl to select the user name and password from your environment
by simply specifying a single colon with this option: "-u :" or by specfying the login options on their own, for example "-u ;auth=NTLM".

You can use the optional login options part to specify protocol specific options that may be used during authentication. At present only IMAP, POP3 and
SMTP  support  login options as part of the user login information. For more information about the login options please see RFC 2384, RFC 5092 and IETF
draft draft-earhart-url-smtp-00.txt (Added in 7.31.0).

If this option is used several times, the last one will be used.

Example

$ curl -vX GET http://127.0.0.1:8081/simple/ --user pchero:1234
* Hostname was NOT found in DNS cache
*   Trying 127.0.0.1...
* Connected to 127.0.0.1 (127.0.0.1) port 8081 (#0)
* Server auth using Basic with user 'pchero'
> GET /simple/ HTTP/1.1
> Authorization: Basic cGNoZXJvOjEyMzQ=
> User-Agent: curl/7.35.0
...

-I, --head

(HTTP FTP FILE) Fetch the headers only. HTTP-servers feature the command HEAD which this uses to get nothing but the header of a document. When used on an FTP or FILE file, curl displays the file size and last modification time only.

-L, --location

만약 해당 페이지가 다른 곳으로 옮겨졌을 때(3XX response code), 자동으로 옮겨진 곳으로 다시 request 를 전송한다.

-X, --request <command>

GET, HEAD, POST, PUT 과 같은 Method 방식을 지정한다.

(HTTP) Specifies a custom request method to use when communicating with the HTTP server.  The specified request will be used instead of the method oth‐
erwise  used  (which  defaults  to  GET). Read the HTTP 1.1 specification for details and explanations. Common additional HTTP requests include PUT and
DELETE, but related technologies like WebDAV offers PROPFIND, COPY, MOVE and more.

Normally you don't need this option. All sorts of GET, HEAD, POST and PUT requests are rather invoked by using dedicated command line options.

This option only changes the actual word used in the HTTP request, it does not alter the way curl behaves. So for example if you want to make a  proper
HEAD request, using -X HEAD will not suffice. You need to use the -I, --head option.

(FTP) Specifies a custom FTP command to use instead of LIST when doing file lists with FTP.

(POP3) Specifies a custom POP3 command to use instead of LIST or RETR. (Added in 7.26.0)

(IMAP) Specifies a custom IMAP command to use insead of LIST. (Added in 7.30.0)

(SMTP) Specifies a custom SMTP command to use instead of HELP or VRFY. (Added in 7.34.0)

If this option is used several times, the last one will be used.

Examples

ssl certification no check

<source lang=bash> $ curl -k -v https://web_sites.com </source>

POST with data

<source lang=bash> $ curl 127.0.0.1:8081 -X POST -d 'test data 123' </source>

POST with binary data

<source lang=bash> $ curl -k -X POST -H "Content-Type: application/json" \ --data-binary '{ "message": {"username":"test username","password":"4ff5ff499cf340f13d2c4c95859712da"} }' https://pchero21.com:8080/auth </source>

Shows headers

$ curl -I localhost:8080/login

HTTP/1.1 200 OK
Set-Cookie: cookie-name=MTU1MjQ2NDIwMHxEdi1CQkFFQ180SUFBUkFCRUFBQUpmLUNBQUVHYzNSeWFXNW5EQThBRFdGMWRHaGxiblJwWTJGMFpXUUVZbTl2YkFJQ0FBRT185g9ZNawBhaQfkJPy9RW4fz6mFuZMRT-a8t59QneoCvw=; Path=/; Expires=Fri, 12 Apr 2019 08:03:20 GMT; Max-Age=2592000
Date: Wed, 13 Mar 2019 08:03:20 GMT