Curl使用笔记

Posted by wsxq2 on 2020-04-13
TAGS:  curlHTTPcURLSMTP

本文最后一次编辑时间:2020-04-13 17:56:19 +0800

本文讲述curl工具的相关内容

简介

curl 是一个用于传输数据的工具,支持众多协议(DICT, FILE, FTP, FTPS, GOPHER, HTTP, HTTPS, IMAP, IMAPS, LDAP, LDAPS, POP3, POP3S, RTMP, RTSP, SCP, SFTP, SMTP, SMTPS, TELNET and TFTP)。该命令被设计为无需用户交互即可工作

curl 提供大量有用的功能,如代理支持、用户认证、FTP上传、HTTP POST、SSL连接、Cookies、文件断点续传等等,如你所见,它大量的特征将使你头晕目眩

curl 中关于传输相关的特征基于 libcurl,详情参见man 3 libcurl

——翻译自man curl中的DESCRIPTION部分

HTTP

GET

1
2
curl -i -H "Accept: application/json" -H "Content-Type: application/json" -X GET http://hostname/resource
curl -H "Accept: application/xml" -H "Content-Type: application/xml" -X GET http://hostname/resource

——引用自https://stackoverflow.com/a/14978657

POST

For posting data:

1
curl --data "param1=value1&param2=value2" http://hostname/resource

For file upload:

1
curl --form "fileupload=@filename.txt" http://hostname/resource

RESTful HTTP Post:

1
curl -X POST -d @filename http://hostname/resource

For logging into a site (auth):

1
2
curl -d "username=admin&password=admin&submit=Login" --dump-header headers http://localhost/Login
curl -L -b headers http://localhost/

——引用自https://stackoverflow.com/a/14978657

Pretty Printing

JSON

Python

If you use pip and python, you can install pjson package by running this command:

1
pip install pjson

Usage:

1
curl -i -H "Accept: application/json" -H "Content-Type: application/json" -X GET http://hostname/resource | pjson

If you use Python 2.6+, json tool is bundled within.

Usage:

1
curl -i -H "Accept: application/json" -H "Content-Type: application/json" -X GET http://hostname/resource | python -m json.tool

——引用自https://stackoverflow.com/a/14978657

链接

下面总结了本文中使用的所有链接:

引用