curl

https://www.gitbook.com/download/pdf/book/bagder/everything-curl

download file
$curl -O http://www.asipress.ir/en/index.txt
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100 24632    0 24632    0     0  22433      0 --:--:--  0:00:01 --:--:-- 22453

-------------------------
if you pressed ctrl+C, you can try to resume stopped session
Using curl -C option, you can continue a download which was stopped already for some reason.

$ curl -C - -O http://www.asipress.ir/en/index.txt
** Resuming transfer from byte position 16384
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0
curl: (33) HTTP server doesn't seem to support byte ranges. Cannot resume.
-------------------------
$ curl -u username:password URL

By default curl uses Basic HTTP Authentication. We can specify other authentication method using –ntlm | –digest.
-------------------------
curl can be used to download files from FTP server
$ curl -u ftpuser:ftppass -O ftp://ftp_server/public_html/xss.php
-----------------------
use proxy to download
$ curl -x proxysever.test.com:3128 http://google.co.in
-----------------------
curl can be used to send a mail
$ curl --mail-from blah@test.com --mail-rcpt foo@test.com smtp://mailserver.com
-----------------------

Using JSON
Here is a simple example of a command that can be used to update an issue:

$ curl -v -H "Content-Type: application/json" -X PUT --data "@388.json" -u login:password http://redmine/issues/388.json
$ curl -v -H "Content-Type: application/json" -X PUT --data "@388.json" -H "X-Redmine-API-Key: xxxx" http://redmine/issues/388.json

The file that contains the data sent to Redmine (388.json in the example above) would look like this:

{
  "issue": {
    "subject": "subject123",
    "notes": "Changing the subject"
  }
}
Note: it's required to set the Content-Type header according to the format of the data you are sending. It must be set to one the following values:
application/json
application/xml

Using XML
Here is a simple example of a command that can be used to create an issue with custom fields:

$ curl -v -H "Content-Type: application/xml" -X POST --data "@issue.xml" -u login:password http://redmine/issues.xml
$ curl -v -H "Content-Type: application/xml" -X POST --data "@issue.xml" -H "X-Redmine-API-Key: xxxx" http://redmine/issues.xml

Where issue.xml content is:

<?xml version="1.0" encoding="ISO-8859-1" ?>
<issue>
  <subject>API custom fields</subject>
  <project_id>1</project_id>
  <tracker_id>2</tracker_id>
  <custom_fields type="array">
    <custom_field>
      <id>2</id>
      <value>Fixed</value>
    </custom_field>
    <custom_field>
      <id>1</id>
      <value>0.8.2</value>
    </custom_field>
  </custom_fields>
</issue>
Text formatting
If you want to use some text formatting (e.g to update a wiki page on your project), you should use the curl's option --data-binary instead of --data to load the file.

$ curl -v -H "Content-Type: application/xml" -X PUT --data-binary "@wiki.xml" -u login:password http://redmine/projects/foo/wiki/page_test.xml

Where wiki.xml content is:

<?xml version="1.0"?>
<wiki_page>
<text>
h1. TITLE

 %{font-size:14pt}SUBTITLE%
</text>
</wiki_page>
Attaching files
If you want to create an issue with image.png attached, you need to upload this file first:

$ curl --data-binary "@image.png" -H "Content-Type: application/octet-stream" -X POST -u login:password http://redmine/uploads.xml

# 201 response
<upload>
  <token>7167.ed1ccdb093229ca1bd0b043618d88743</token>
</upload>
Then, use the token to create the issue:

$ curl -v -H "Content-Type: application/xml" -X POST --data "@issue.xml" -u login:password http://redmine/issues.xml

Where issue.xml content is:

<?xml version="1.0" encoding="ISO-8859-1" ?>
<issue>
  <subject>Issue with attachment</subject>
  <project_id>1</project_id>
  <uploads type="array">
    <upload>
      <token>7167.ed1ccdb093229ca1bd0b043618d88743</token>
      <filename>image.png</filename>
      <content_type>image/png</content_type>
    </upload>
  </uploads>
</issue>
-------------------------------
You can also use cURL to perform an HTTP POST operation, which is what most HTML-based forms use to send data to a remote server. There are a few ways to tell cURL what data to send when doing a POST operation, but this article describes how to use the file-based method because many of the POST operations of the Rackspace Cloud API involve sending JSON or XML documents. To post an XML or JSON file, you need to specify the Content-Type appropriately so that the receiving server knows what to expect, as shown in the following examples:

$ curl -X POST -d @mydocument.xml -H "Content-Type: application/xml" http://www.example.com/form
$ curl -X POST -d @mydocument.json -H "Content-Type: application/json" http://www.example.com/form

You can also specify the data as a quoted string, but this can be unwieldy when done from the command line.

----------------------------------
Viewing the HTTP headers

Many operations do not return a response body, just response headers. Much of the usefulness of the API result is in the headers, which contain useful information such as the HTTP response code. To view the HTTP response headers, use the -I option, as shown in the following example. This option is the equivalent of an HTTP HEAD request (X HEAD):

$ curl -I http://www.example.com
HTTP/1.0 302 Found
Location: http://www.iana.org/domains/example/
Server: BigIP
Connection: Keep-Alive
Content-Length: 0

Viewing More HTTP Debug Information

At times, it is useful to view the full HTTP request/response transaction. You can do this by using the verbose flag, which prints out practically all the HTTP data that is sent back and forth, including the request headers. This flag is quite useful for debugging purposes.

$ curl -v http://www.example.com
* About to connect() to www.example.com port 80 (#0)
* Trying 192.0.43.10... connected
* Connected to www.example.com (192.0.43.10) port 80 (#0)
> GET / HTTP/1.1
-------------------------------
http://www.rackspace.com/knowledge_center/article/cloud-files-curl-cookbook



Comments

Popular posts from this blog

HAproxy logging

tomcat catalina coyote jasper cluster

NFS mount add in fstab _netdev instead of default | firewall-cmd --list-all