curl
Other
- 下载文件
- curl -O http://example.com/file.zip
- 使用代理
- curl -x proxy.example.com:8080 http://example.com/api
- 跟踪重定向
- curl -L http://example.com
- 限速下载
- curl --limit-rate 100K http://example.com/file.zip
- Cookie
- curl -b "cookie1=value1; cookie2=value2" http://example.com/api
http 请求
设置请求头
shell
curl -H "Authorization: Bearer token" http://example.com/api发送 JSON
shell
curl -X POST -H "Content-Type: application/json" -d '{"key1":"value1","key2":"value2"}' http://example.com/apiGet
要使用curl发送GET请求并带参数,您可以在URL中附加查询字符串参数。以下是一个示例命令:
shell
curl -X GET "https://example.com/api?param1=value1¶m2=value2"URL 中的 & 等字符需要用引号保护;参数值中的空格与特殊字符需要 URL 编码。
post
shell
curl -X POST -H "Content-Type: application/json" -d '{"key1": "value1", "key2": "value2"}' https://example.com/api/endpoint- 格式化输出
shell
curl -X POST -H "Content-Type: application/json" -d '{"key1": "value1", "key2": "value2"}' https://example.com/api/endpoint | jqfrom 要使用cURL进行POST请求发送表单数据,可以使用
-d或--data选项,后跟以key=value格式表示的表数据。以下是一个示:bashcurl -X POST -d "username=johndoe&password=secretpassword" https://example.com/login在这个示例,我们向
https://example.com/login发送一个带有两个表单字段username和password的POST请求。如果有多个单字段,可以使用与号(
&)分隔们。如果需要,确保正确地对值进行URL编码。此外,可以使用
-H``--header选项定请求的内容类型。例如,如果要使用application/x-www-form-urlencoded内容类型发送请求,可以添加以下标头:bashcurl -X POST -H "Content-Type: application/x-www-form-urlencoded" -d "username=johndoe&password=secretpassword" https://example.com/login请记得将
https://example.com/login替换为实际发送请求的URL
继续阅读
- 知识专题
- 知识专题
- 应用实践