Google Prettify

2023-05-31

如何使用CURL來進行web service測試

輸入參數 

curl是linux上一個測試連線的工具。如果輸入參數很簡單,我們可以直接下參數指令進行測試:

$ curl -X POST -H "Content-Type: application/json" -d '{"status" : false, "name" : "Jack"}' "http://www.example.com/api/resources"

當輸入參數是XML或json格式時,可以將輸入內容存放在一個檔案中,再進行測試。

curl --header "content-type: text/soap+xml; charset=utf-8"--data @sample.xml http://$HOSTNAME:$PORT/$SOMEPATH

sample.xml
<?xml version='1.0' encoding='utf-8'?>
<soapenv:Envelope xmlns:soapenv="http://www.w3.org/2003/05/soap-envelope">
  <soapenv:Body>
    <ns0:some_operation xmlns:ns0="http://some_ns_uri"/>
  </soapenv:Body>
</soapenv:Envelope>

輸入帳密

若所存取網頁有使用 Basic Authentication認證,可以使用 `--user username:password` 來通過驗證:

$ curl -i --user secret:vary_secret http://www.example.com/api/resources


您或許對這些有興趣

最後

謝謝您的閱讀,希望您可以有豐富的收獲。