一、curl發送get請求帶參數
在HTTP請求中,GET方法用於請求指定的資源。通過URL傳遞參數,GET請求常用於獲取數據。
使用curl發送帶參數的GET請求十分簡單。只需在URL上添加參數即可。
curl "https://example.com/api/user?name=john&age=18"
上述代碼向`https://example.com/api/user`發送一個GET請求,參數為`name=john`和`age=18`。
二、curl發送get帶參數
get參數也可以通過在curl命令後附加`-d`選項發送。此選項通常用於發送POST請求的數據。
curl -G "https://example.com/api/user" --data-urlencode "name=john" --data-urlencode "age=18"
上述代碼使用`-G`選項發送GET請求,並使用`–data-urlencode`選項將參數添加到URL。
三、curl發送get請求返回400
當服務器無法處理或理解請求時,會返回HTTP狀態碼400。
在curl命令中,我們可以使用`-v`選項查看完整的HTTP響應。如果返回碼為400,可能需要檢查URL或參數是否正確。
curl -v "https://example.com/api/user?name=***"
上述代碼向`https://example.com/api/user`發送一個GET請求,但參數`name`被替換為不存在的值。curl將顯示完整的HTTP響應,包括狀態碼。
四、curl發送get請求帶param
有時候我們需要向多個API發送相同的參數,而這些API可能具有不同的名稱。我們可以使用curl的`–data`選項傳遞參數數組。
curl "https://example.com/api/user" --data "param[]=john¶m[]=18"
上述代碼向`https://example.com/api/user`發送一個GET請求,參數為`param[]=john`和`param[]=18`。
五、curl發送get請求帶多個head參數
HTTP請求頭是指向服務器發送的請求附加的額外信息。我們可以使用curl發送包含多個頭部的HTTP請求。
curl -H "Accept-Language: en-US,en;q=0.8" -H "Connection: keep-alive" "https://example.com/api/user"
上述代碼通過傳遞頭部參數`Accept-Language`和`Connection`向`https://example.com/api/user`發送GET請求。
六、curl發送get請求攜帶header參數
有時候,我們需要在curl請求中添加自定義的HTTP頭。可以使用`-H`選項來添加自定義HTTP頭。
curl -H "Authorization: Bearer xxx" "https://example.com/api/user"
上述代碼向`https://example.com/api/user`發送GET請求,並添加了帶有Bearer令牌的HTTP頭部。
七、curl發送post請求報文
curl支持HTTP POST請求,以發送包含在請求體中的數據。
curl -X POST -d '{"name":"john", "age":18}' -H "Content-Type: application/json" "https://example.com/api/user"
上述代碼向`https://example.com/api/user`發送一個POST請求,請求體為JSON字符串`{‘name’: ‘john’, ‘age’: 18}`,Content-Type則被設置為`application/json`。
八、curl get請求傳遞參數
在發送GET請求時,我們可以使用`–data`選項將數據附加到URL查詢中。
curl -G "https://example.com/api/user" --data "name=john" --data "age=18"
上述代碼向`https://example.com/api/user`發送一個GET請求,帶有`name=john`和`age=18`的查詢參數。
九、curl delete請求
在HTTP協議中,DELETE方法在服務器上刪除指定的資源。
curl可以像發送GET和POST請求一樣發送DELETE請求。我們可以使用`-X`選項指定請求方法,使用`-d`選項發送請求數據。
curl -X DELETE -d "id=1" "https://example.com/api/user"
上述代碼向`https://example.com/api/user`發送一個DELETE請求,請求體是`id=1`。
原創文章,作者:CKZJ,如若轉載,請註明出處:https://www.506064.com/zh-hk/n/138489.html