curl

http://www.lornajane.net/posts/2008/Curl-Cheat-Sheet

http://www.lornajane.net/posts/2010/Curl-and-Cookies

http://www.cantoni.org/2012/01/10/curl-cheat-sheet

http://www.hurl.it/

http://restify.io/



Curl Cheat Sheet

I have a scribbled sheet on my desk, which is my "cheat sheet" for curl, its really short and I thought I'd put my notes here for safe-keeping. If you're visiting, then I hope they help you too.


Curl from command line

Curl is a way of making web requests from the command line. I only do this under linux, and if you don't know why you'd want to do that, then you probably don't need to read any further (although, that would also make an interesting article!).

curl http://www.lornajane.net

Will give you the output that would be sent to your browser if you requested the main page of this site from there.

Verbose output (-v)

This is where it gets interesting, you can see the actual information being included with the HTTP request when you use the verbose switch.

curl -v http://www.lornajane.net

Try it - you see the headers and the client/server negotiation. Stuff like cache information, browser identification, and cookie information all gets transmitted in these headers.

Using HTTP verbs (-X) and data (-d)

Curl can make requests and include data with them. It can use GET as you'd expect, but POST, PUT and DELETE are all supported too.

curl -X GET "http://www.example.com?page=2&category=toys"

I've added the quotes because the ampersand confuses the command line

curl -X POST http://www.example.com/registration.php -d username=lornajane -d password=password -d phone=0123456789

PUT and DELETE can be done in the same way, just using the -X switch.

Further Resources

Actually, if you've got this far then you know what you're doing - use the curl manpage for information on the other switches. Curl can do more things than I think I would ever want to do, not just HTTP, and can spoof cookies and all sorts of other good things. Its especially good for testing web services as it allows clearer diagnosis of the exact behaviour and response. Don't tell anyone but I have also been known to vardump in the middle of webservices when debugging, and using curl lets you see the output "as it comes" - very handy!

Update: I wrote a follow-up post about Using Cookies with Curl

Curl and Cookies

I noticed the other day that the cheat sheet I have on this site for curl doesn't show how to use cookies, so I thought I'd remedy that omission, and quickly! Being able to use the command line to authenticate and then go on and use part of a site behind a login box can be really handy, and it is also super-simple.

-c to Save a Cookie

Pass the -c switch followed by a filename and curl will write the cookies to a file. This is the "cookie jar" and you can dip into it whenever you want to send the cookies back with a future request. For example:
curl -c cookies.txt http://www.lornajane.net
This writes a file named cookies.txt to the local directory. When I look in it, it contains:

# Netscape HTTP Cookie File
# http://curl.haxx.se/rfc/cookie_spec.html
# This file was generated by libcurl! Edit at your own risk.

www.lornajane.net FALSE / FALSE 0 s9y_4e071c5ccc553288993faf0369cb076c 539e01676501366ea0f04e2646b1a31d

-b to Send Cookies

All I do when I want to use the cookie on future requests is pass exactly the same command but with a -b switch; this will read the named file and send the cookies along. You can edit the cookies as you wish, at your own risk of course, and this makes the use of cookies and curl an absolutely invaluable technique for testing! It's also common to use it on sites where you want to download a file directly to the server but the site requires login first.



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值