[Mac/Linux] 使用 curlrc 檔案設定 curl 預設參數

[Mac/Linux] 使用 curlrc 檔案設定 curl 預設參數

最近遇到一個問題,有個程式會去呼叫 curl 去連線遠端網站,

不過有時候會失敗,

但只看的到最終的結果,看不到連線過程,

不確定它送出的 HTTP 標頭等等有無問題…

 

舉例來說,假設我們手動跑 curl 去連 tw.yahoo.com,

那它可能就只回傳一個 redirect,

你也搞不清楚這究竟是為什麼:

# curl tw.yahoo.com
redirect

 

如果 curl 是自己呼叫的,自然就可以自己加參數。

可是現在這個 curl 是別人的程式呼叫的,我也改不到這程式,怎麼辦呢?

 

查了一下,可以試試看 curl 的預設設定檔~

在 Mac/Linux 上的話,預設會去讀取 ~/.curlrc 這個檔案。

因此,我們可以設定它的內容是  -v  來顯示更多訊息:

echo -v > ~/.curlrc

 

這樣的話,只要程式是使用目前使用者身分執行的話,

它應該就會使用到這個設定檔,

而自動加上 -v 這個參數的效果了:

# curl tw.yahoo.com
* About to connect() to tw.yahoo.com port 80 (#0)
*   Trying 202.165.107.50...
* Connected to tw.yahoo.com (202.165.107.50) port 80 (#0)
> GET / HTTP/1.1
> User-Agent: curl/7.29.0
> Host: tw.yahoo.com
> Accept: */*
>
< HTTP/1.1 301 Moved Permanently
< Date: Fri, 08 Jan 2021 06:39:56 GMT
< Connection: keep-alive
< Server: ATS
< Cache-Control: no-store, no-cache
< Content-Type: text/html
< Content-Language: en
< Content-Security-Policy: frame-ancestors 'self' https://*.techcrunch.com https://*.yahoo.com https://*.aol.com https://*.huffingtonpost.com https://*.oath.com https://*.search.yahoo.com https://*.search.aol.com https://*.search.huffpost.com https://*.verizonmedia.com https://*.publishing.oath.com https://*.autoblog.com; sandbox allow-forms allow-same-origin allow-scripts allow-popups allow-popups-to-escape-sandbox allow-presentation; report-uri https://csp.yahoo.com/beacon/csp?src=ats&site=frontpage&region=TW&lang=zh-Hant-TW&device=desktop&yrid=&partner=;
< X-Frame-Options: SAMEORIGIN
< Location: https://tw.yahoo.com/
< Content-Length: 8
<
* Connection #0 to host tw.yahoo.com left intact
redirect

 

如上,我們就可以知道,

curl 其實是遇上了 HTTP 301 Moved Permanently 重導向,

因此只回了短短的 redirect 在 HTTP body 裡面囉~

 

參考資料:

(本頁面已被瀏覽過 661 次)

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *

這個網站採用 Akismet 服務減少垃圾留言。進一步了解 Akismet 如何處理網站訪客的留言資料