2022年9月15日 星期四

cURL 使用筆記

https://curl.se/

最近在工作上碰到有腳本使用 cURL來即時向 server請求資料,趁此紀錄一些其使用方法。

HTTP Request


curl -X [GET|POST|PUT|DELETE|PATCH] URL

-X 參數用來對 URL發出 HTTP request,後面帶入HTTP method。


curl -X [GET|POST|PUT|DELETE|PATCH] URL -x PROXY_ADDRESS:PROXY_PORT

-x 通過代理伺服器 PROXY_ADDRESS:PROXY_PORT使用 cURL。

2022年9月3日 星期六

jq - 輕量級 json解析工具

官方網址:

https://stedolan.github.io/jq
github
https://jqplay.org

最近工作上接觸到其他人寫的 script,使用 jq來將 curl所得到的 .json檔案解析成 .csv檔。
雖然目前只使用到 jq一點點功能,但還是趁此機會紀錄一下。

依照官網的範例,可透過 github的 api獲取 json格式的 jq最近 5個 commit資訊:

curl 'https://api.github.com/repos/stedolan/jq/commits?per_page=5'

使用 jq過濾 json:

curl 'https://api.github.com/repos/stedolan/jq/commits?per_page=5' | jq '.'

只取出第一個 commit的資訊:

curl 'https://api.github.com/repos/stedolan/jq/commits?per_page=5' | jq '.[0]'

取出所有 commit中的 sha資訊:

curl 'https://api.github.com/repos/stedolan/jq/commits?per_page=5' | jq '.[] | {sha: .sha}'

{
  "sha": "cff5336ec71b6fee396a95bb0e4bea365e0cd1e8"
}
{
  "sha": "f2ad9517c72f6267ae317639ab56bbfd4a8653d4"
}
{
  "sha": "c4d39c4d22f2b12225ca1b311708f7e084ad9ff8"
}
{
  "sha": "174db0f93552bdb551ae1f3c5c64744df0ad8e2f"
}
{
  "sha": "29cf77977ef52eec708982b19bf9d2ec17443337"
}


jqplay

也可以使用 jqplay的網站來嘗試 jq的輸出結果: