Post

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 请求

设置请求头

1
2
curl -H "Authorization: Bearer token" http://example.com/api

发送文件

1
2
curl -X POST -H "Content-Type: application/json" -d '{"key1":"value1","key2":"value2"}' http://example.com/api

Get

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

1
curl -X GET "https://example.com/api?param1=value1&param2=value2"

在上面的例中,您需要https://example.com/api 替换实际的API端URL,并将param1=value1&param2=value2替换您要发送的参数。

如果您的参数值包含特殊字符或空格,您可能需要对其进行URL编码。您可以使用urlencode命令行工或在线URL编码工具来执行此操作。

请注意,些特殊字符(如&?等)在命令行中具有特含义,因此您可能需要使用引号将整个参数部分括起来,以避免解析错误。

另外,请保您已经安装了curl命令行工具,并且可以从端或命令提示符中运行它。

post

1
  curl -X POST -H "Content-Type: application/json" -d '{"key1 "value1", "key2": "value2"}' https://example.com/api/endpoint
  • 格式化输出
1
  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格式表示的表数据。以下是一个示:

    1
    
    curl -X POST -d "username=johndoe&password=secretpassword" https://example.com/login
    

    在这个示例,我们向https://example.com/login 发送一个带有两个表单字段usernamepassword的POST请求。

    如果有多个单字段,可以使用与号(&)分隔们。如果需要,确保正确地对值进行URL编码。

    此外,可以使用-H--header选项定请求的内容类型。例如,如果要使用application/x-www-form-urlencoded内容类型发送请求,可以添加以下标头:

    1
    
    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

This post is licensed under CC BY 4.0 by the author.