当前位置: 代码网 > 服务器>服务器>Linux > 如何使用curl命令发送POST请求(带token)

如何使用curl命令发送POST请求(带token)

2025年04月01日 Linux 我要评论
使用curl 命令发送post请求记录curl命令使用说明。curl命令简介curl 是常用的命令行工具,用来请求 web 服务器。它的名字就是客户端(client)的 url 工具的意思。它的功能非

使用curl 命令发送post请求

记录curl命令使用说明。

curl命令简介

  • curl 是常用的命令行工具,用来请求 web 服务器。
  • 它的名字就是客户端(client)的 url 工具的意思。它的功能非常强大,命令行参数多达几十种。
  • 如果熟练的话,完全可以取代 postman 这一类的图形界面工具。

curl命令发送请求(带token)

curl命令发送post请求:

参数内容:

  • -h 请求头
  • -d post内容
  • -x 请求协议
curl -x post -h 'authorization:{{token}}' -h 'content-type: application/json' -d '{"name": "viptest1","vpcid": "1546774307671072768","subnetid": "96e6148f-486b-41ce-8694-16f249002d96","fixedip": "192.168.10.222"}' localhost:8080/vpc/v1/vips

curl命令发送get请求:

curl -x get -h 'authorization:{{token}}' localhost:8080/vpc/v1/vips

curl命令行发送post/get请求

curl命令是一个利用url规则在命令行下工作的文件传输工具。使用一种受支持的协议,从远程服务器传输数据,或将数据传输到远程服务器。

默认情况下,已安装在macos和大多数linux发行版上。curl支持包括http、https、ftp等众多协议,还支持post、cookies、认证、从指定偏移处下载部分文件、用户代理字符串、限速、文件大小、进度条等特征。

在进行web后台程序开发测试过程中,常常会需要发送url进行测试,使用curl可以方便地模拟出符合需求的url命令。

curl发送get请求

  • 语法格式:
curl protocol://address:port/url?args 
  • 示例:
curl http://127.0.0.1:8080/login?admin&passwd=12345678  # 带参数请求,参数用&连接

curl发送post请求

  • 语法格式:
curl -x post [options] [url]
 
# 使用该-f选项时,curl使用的默认content-type是“multipart/form-data”,以key=value配对形式
curl -x post -f 'name=jason' -f 'email=jason@example.com' http://127.0.0.1:8000/login
 
# 使用-d选项,可以使用&符号对发送数据进行合并
curl -x post -d 'name=jason&email=jason@example.com' http://127.0.0.1:8000/login
 
# 使用-h选项,指定content-type为application/json发送json对象
curl -x post -h "content-type:application/json"  -d '{"user": "admin", "passwd":"12345678"}' http://127.0.0.1:8000/login  # 通过-d指定json data内容
 
# 文件上传,需在文件位置之前添加@符号
curl -x post -f 'image=@/home/user/pictures/wallpaper.jpg' http://example.com/upload

总结

以上为个人经验,希望能给大家一个参考,也希望大家多多支持代码网。

(0)

相关文章:

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2025  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com