用 postman 學(xué) cURL

不用 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í)踐

  1. 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ì).

  1. 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
  1. 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è)文件上傳
收到的 request

注意到上面最后一個(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

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末嬉愧,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子道伟,更是在濱河造成了極大的恐慌,老刑警劉巖,帶你破解...
    沈念sama閱讀 218,607評(píng)論 6 507
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場(chǎng)離奇詭異忍燥,居然都是意外死亡,警方通過(guò)查閱死者的電腦和手機(jī)隙姿,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,239評(píng)論 3 395
  • 文/潘曉璐 我一進(jìn)店門梅垄,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái),“玉大人输玷,你說(shuō)我怎么就攤上這事队丝。” “怎么了欲鹏?”我有些...
    開(kāi)封第一講書人閱讀 164,960評(píng)論 0 355
  • 文/不壞的土叔 我叫張陵机久,是天一觀的道長(zhǎng)。 經(jīng)常有香客問(wèn)我赔嚎,道長(zhǎng)膘盖,這世上最難降的妖魔是什么? 我笑而不...
    開(kāi)封第一講書人閱讀 58,750評(píng)論 1 294
  • 正文 為了忘掉前任尤误,我火速辦了婚禮侠畔,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘损晤。我一直安慰自己软棺,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,764評(píng)論 6 392
  • 文/花漫 我一把揭開(kāi)白布尤勋。 她就那樣靜靜地躺著喘落,像睡著了一般。 火紅的嫁衣襯著肌膚如雪最冰。 梳的紋絲不亂的頭發(fā)上瘦棋,一...
    開(kāi)封第一講書人閱讀 51,604評(píng)論 1 305
  • 那天,我揣著相機(jī)與錄音锌奴,去河邊找鬼兽狭。 笑死,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的箕慧。 我是一名探鬼主播服球,決...
    沈念sama閱讀 40,347評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開(kāi)眼,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼颠焦!你這毒婦竟也來(lái)了斩熊?” 一聲冷哼從身側(cè)響起,我...
    開(kāi)封第一講書人閱讀 39,253評(píng)論 0 276
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤伐庭,失蹤者是張志新(化名)和其女友劉穎粉渠,沒(méi)想到半個(gè)月后,有當(dāng)?shù)厝嗽跇?shù)林里發(fā)現(xiàn)了一具尸體圾另,經(jīng)...
    沈念sama閱讀 45,702評(píng)論 1 315
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡霸株,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,893評(píng)論 3 336
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了集乔。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片去件。...
    茶點(diǎn)故事閱讀 40,015評(píng)論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖扰路,靈堂內(nèi)的尸體忽然破棺而出尤溜,到底是詐尸還是另有隱情,我是刑警寧澤汗唱,帶...
    沈念sama閱讀 35,734評(píng)論 5 346
  • 正文 年R本政府宣布宫莱,位于F島的核電站,受9級(jí)特大地震影響哩罪,放射性物質(zhì)發(fā)生泄漏授霸。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,352評(píng)論 3 330
  • 文/蒙蒙 一识椰、第九天 我趴在偏房一處隱蔽的房頂上張望绝葡。 院中可真熱鬧,春花似錦腹鹉、人聲如沸。這莊子的主人今日做“春日...
    開(kāi)封第一講書人閱讀 31,934評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)。三九已至绞蹦,卻和暖如春力奋,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背幽七。 一陣腳步聲響...
    開(kāi)封第一講書人閱讀 33,052評(píng)論 1 270
  • 我被黑心中介騙來(lái)泰國(guó)打工景殷, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 48,216評(píng)論 3 371
  • 正文 我出身青樓猿挚,卻偏偏與公主長(zhǎng)得像咐旧,于是被迫代替她去往敵國(guó)和親。 傳聞我的和親對(duì)象是個(gè)殘疾皇子绩蜻,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,969評(píng)論 2 355

推薦閱讀更多精彩內(nèi)容

  • Spring Cloud為開(kāi)發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見(jiàn)模式的工具(例如配置管理铣墨,服務(wù)發(fā)現(xiàn),斷路器办绝,智...
    卡卡羅2017閱讀 134,657評(píng)論 18 139
  • 1.引言 最近在嘗試android系統(tǒng)啟動(dòng)的時(shí)候打印log伊约。看看啟動(dòng)順序是不是按照書上講的那個(gè)樣子孕蝉。在用adb啟動(dòng)...
    過(guò)期的薯?xiàng)l閱讀 2,340評(píng)論 0 0
  • 2017年屡律,10月7號(hào),雨 我就上午本打算出門買個(gè)藥就回來(lái)呢降淮,可平吵瘢總覺(jué)得頸椎肩頸疼的厲害,而且都不敢用右手抱...
    放飛夢(mèng)想a閱讀 237評(píng)論 0 0
  • 9月8日凌晨,薛之謙在微博上發(fā)布昏黃路燈下?tīng)渴终掌傅撸⒄f(shuō):我記得…你跟我時(shí)…我一無(wú)所有…我不想再尋覓了….請(qǐng)讓我給...
    一禾君儀閱讀 583評(píng)論 0 3