Skip to content

curl ​

Other ​

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/api

Get ​

要使用curl发送GET请求并带参数,您可以在URL中附加查询字符串参数。以下是一个示例命令:

shell
curl -X GET "https://example.com/api?param1=value1&param2=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 | jq
  • from 要使用cURL进行POST请求发送表单数据,可以使用-d或--data选项,后跟以key=value格式表示的表数据。以下是一个示:

    bash
    curl -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内容类型发送请求,可以添加以下标头:

    bash
    curl -X POST -H "Content-Type: application/x-www-form-urlencoded" -d "username=johndoe&password=secretpassword" https://example.com/login

    请记得将https://example.com/login 替换为实际发送请求的URL

继续阅读