不用 postman 也能學(xué), 但用 postman 更簡(jiǎn)單.
目標(biāo)
目標(biāo)很簡(jiǎn)單, 也很明確, 能夠使用 cURL 發(fā)送常用的 GET, POST, DELETE, PATCH 請(qǐng)求.
方法
首先打開(kāi) postman, 在地址欄輸入 https://requestb.in/s437tns4. 這是我從 https://requestb.in/ 臨時(shí)生成的一個(gè) url, 可以用來(lái)接受并顯示我們的 request. 它就像一個(gè)回聲, 你 ping 它就 pong.
點(diǎn)擊發(fā)送, 有:
看到已經(jīng)返回了狀態(tài)碼 200 以及 ok.
到 request bin 刷新一下, 可以看到剛才從 postman 發(fā)出的請(qǐng)求:
下面用 cURL 來(lái)做同樣的事. 點(diǎn)擊 postman 右上角的 code
, 可以看到這個(gè) request 在多種語(yǔ)言的代碼, 我們選擇 cURL, 得到代碼:
curl -X GET \
https://requestb.in/s437tns4 \
-H 'cache-control: no-cache' \
-H 'postman-token: 5b7cd4ba-3dd2-f7e6-d793-c6a1aef2f36e'
在 shell 下發(fā)送這個(gè)請(qǐng)求:
再去 request bin 刷新一下, 收到了第二個(gè)請(qǐng)求!
截了這么久的圖... 現(xiàn)在我們學(xué)到了三個(gè)知識(shí)點(diǎn):
- curl 的一般格式是
curl <一個(gè)url>
- 用
-X GET
指定 request 類型位 GET 請(qǐng)求 (An HTTP Method (verb)) - 用
-H 'key=value'
指定 header 鍵值對(duì)
實(shí)踐
過(guò)程你懂了. 現(xiàn)在把 postman 的 GET 換成 POST, 得到 cURL 代碼:
curl -X POST \
https://requestb.in/s437tns4 \
-H 'cache-control: no-cache' \
-H 'postman-token: a645c3f7-fe70-3e2a-213e-5651826cd735'
再刷新 request bin 頁(yè)面, 得到一個(gè) post 請(qǐng)求:
但你啥東西都沒(méi)有 post 啊, 我們給 body 加上點(diǎn)文本:
curl -X POST \
https://requestb.in/s437tns4 \
-H 'cache-control: no-cache' \
-H 'postman-token: f5cdd615-db24-0add-d278-8928560b1313' \
-d '{
"ping": "pong"
}'
噠噠, 收到了數(shù)據(jù):
現(xiàn)在, 我們很確定 -X
后面指定的是 request 方法. 還學(xué)到了加 payload 的方法: -d '<字符串>'
. (這個(gè) -d
是 --data
的意思.)
The request body can be in multiple formats. These formats are defined by the MIME type of the request. The MIME Type can be set using the Content-Type HTTP header. The most commonly used MIME types are:
- multipart/form-data
- application/x-www-form-urlencoded
- application/json
有幾種 post:
- POST Raw Text:
-d 'string'
- POST Form Data:
-d 'foo=bar&foo2=baz'
再多一點(diǎn)實(shí)踐
- form data
curl -X PATCH \
https://requestb.in/s437tns4 \
-H 'cache-control: no-cache' \
-H 'content-type: multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW' \
-H 'postman-token: 17a4727a-cfbd-8ec3-e139-e484270158b2' \
-F ping=pong
哈哈, 我居然是 patch 的一個(gè) form data... 好像哪里不對(duì).
- form url encoded
curl -X POST \
https://requestb.in/s437tns4 \
-H 'cache-control: no-cache' \
-H 'content-type: application/x-www-form-urlencoded' \
-H 'postman-token: 1b1ccc0d-e3a4-73ed-366c-b9d30dc9238c' \
-d '%E6%88%91%E6%98%AF=%E8%B0%81%3F&Who-Am=I'
看看 request bin 收到的 request:
再舉一個(gè)例子:
HTML 里的這樣一個(gè) POST
<form method="POST" action="junk.cgi">
<input type=text name="birthyear">
<input type=submit name=press value=" OK ">
</form>
等價(jià)于
curl --data "birthyear=1905&press=%20OK%20" \
http://www.example.com/when.cgi
This kind of POST will use the Content-Type application/x-www-form-urlencoded and is the most widely used POST kind.
這個(gè) %20
很常見(jiàn), 是空格鍵, 看 ASCII 表 就知道.
現(xiàn)在的 cURL 可以不用手工轉(zhuǎn)義:
curl --data-urlencode "name=I am Daniel" \
http://www.example.com
- params
curl -X GET \
'https://requestb.in/s437tns4?%E6%88%91=%E6%98%AF%E8%B0%81&Who=am-I' \
-H 'cache-control: no-cache' \
-H 'postman-token: 1be0f58e-fa8b-9a1e-79e4-2dbae11f14f7' \
-d '%E6%88%91%E6%98%AF=%E8%B0%81%3F&Who-Am=I'
再看再學(xué)把. postman 雖然好用, 但實(shí)在是太卡了! 所以要 GET cURL.
文件上傳
post form-data:
注意到上面最后一個(gè) boundry 后面還有兩個(gè) --
.
對(duì)應(yīng)的 cURL:
curl -X POST \
https://requestb.in/yx5thyyx \
-H 'cache-control: no-cache' \
-H 'content-type: multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW' \
-H 'postman-token: 8a19e97d-2cac-2783-402e-b0a1b17f048b' \
-F filekey1=@/private/tmp/file1 \
-F filekey2=@/private/tmp/file2.json
那個(gè) boundry 是個(gè) 隨機(jī) 的.
put file:
Perhaps the best way to upload data to a HTTP server is to use PUT. Then again, this of course requires that someone put a program or script on the server end that knows how to receive a HTTP PUT stream.
試了一下和 post 看上去沒(méi)啥區(qū)別.
其他
用 referer 來(lái)看人家傻不傻:
A HTTP request may include a 'referer' field (yes it is misspelled, 哈哈, 就是拼錯(cuò)了), which can be used to tell from which URL the client got to this particular resource. Some programs/scripts check the referer field of requests to verify that this wasn't arriving from an external site or an unknown page. While this is a stupid way to check something so easily forged, many scripts still do it. Using curl, you can put anything you want in the referer-field and thus more easily be able to fool the server into serving your request.
Use curl to set the referer field with:
curl --referer http://www.example.come \
http://www.example.com
user agent:
--user-agent
, 還是看傻不傻.
TODO
更多實(shí)例.
Notes
- mulitple requests:
curl -I http://example.com --next http://example.com
PUT
The HTTP PUT request method is similar to HTTP POST. It too is meant to transfer data to a server (and elicit a response). What data is returned depends on the implementation of the server.
A PUT request can pass parameters to the server using "Query String Parameters", as well as the Request Body. For example, in the following raw HTTP request,
PUT /hi/there?hand=wave
PATCH
The HTTP PATCH method is used to update resources on a server. The exact use of PATCH requests depends on the server in question. There are a number of server implementations which handle PATCH differently. Technically, PATCH supports both Query String parameters and a Request Body.
references