Curl的基本用法和API交互
Curl 是一个强大的命令行工具,用于发送和接收数据,通常用于与Web服务器交互。以下是 Curl 在不同场景下的基本用法:
使用 Curl 进行 POST 请求
-
基本的 POST 请求:
curl -X POST -d 'username=admin&password=admin' <http://94.237.58.222:57443/index.php>
- 通过POST请求发送数据(用户名和密码)到指定的URL。
-
返回数据包的详细信息:
curl -X POST -d 'username=admin&password=admin' <http://94.237.58.222:57443/index.php> -i
i
参数会显示HTTP响应头。
-
使用 Cookie 进行请求:
curl -b 'PHPSESSID=u60mm91e0i25mtg9oipgmjn2bf' <http://94.237.58.222:57443/index.php>
b
参数用于指定发送的Cookie。
-
使用自定义HTTP头发送Cookie:
curl -H 'Cookie: PHPSESSID=u60mm91e0i25mtg9oipgmjn2bf' <http://94.237.58.222:57443/index.php>
H
参数用于添加自定义头部。
-
发送JSON数据并使用Cookie:
curl -X POST -d '{"search":"London"}' -b 'PHPSESSID=u60mm91e0i25mtg9oipgmjn2bf' -H 'Content-Type: application/json' <http://94.237.58.222:57443/search.php>
- 这个命令结合使用了Cookie和JSON数据,用于与Web应用交互。
Curl 与 API 交互
-
读取数据:
curl <http://83.136.255.41:38573/api.php/city/london> | jq
- 使用 Curl 读取数据,并使用
jq
工具格式化JSON输出。 - 也可以不指定特定城市,以检索所有城市的数据。
- 使用 Curl 读取数据,并使用
-
写入数据:
curl -X POST <http://83.136.255.41:38573/api.php/city/> -d '{"city_name":"HTB City", "country_name":"HTB"}' -H 'Content-Type: application/json'
- 使用POST方法和JSON数据格式向API写入新数据。
-
更新数据:
curl -X PUT <http://83.136.255.41:38573/api.php/city/london> -d '{"city_name":"New HTB City", "country_name":"HTB"}' -H 'Content-Type: application/json'
- 使用PUT方法更新特定记录,此例中是更新伦敦的信息。
-
删除数据:
curl -X DELETE <http://83.136.255.41:38573/api.php/city/New> HTB City
- 使用DELETE方法删除特定的数据记录。