CURL POST
CURL POST
https://gist.github.com/subfuzion/08c5d85437d5d4f00e58
https://superuser.com/questions/149329/what-is-the-curl-command-line-syntax-to-do-a-post-request
https://ec.haxx.se/http/http-post
curl -d '{"key1":"value1", "key2":"value2"}' -H "Content-Type: application/json" -X POST http://localhost:3000/data
curl -d "param1=value1¶m2=value2" -H "Content-Type: application/x-www-form-urlencoded" -X POST http://localhost:3000/data
Common Options
-#, --progress-bar
Make curl display a simple progress bar instead of the more informational standard meter.
-b, --cookie <name=data>
Supply cookie with request. If no =
, then specifies the cookie file to use (see -c
).
-c, --cookie-jar <file name>
File to save response cookies to.
-d, --data <data>
Send specified data in POST request. Details provided below.
-f, --fail
Fail silently (don't output HTML error form if returned).
-F, --form <name=content>
Submit form data.
-H, --header <header>
Headers to supply with request.
-i, --include
Include HTTP headers in the output.
-I, --head
Fetch headers only.
-k, --insecure
Allow insecure connections to succeed.
-L, --location
Follow redirects.
-o, --output <file>
Write output to --create-dirs
in conjunction with this to create any directories
specified in the -o
path.
-O, --remote-name
Write output to file named like the remote file (only writes to current directory).
-s, --silent
Silent (quiet) mode. Use with -S
to force it to show errors.
-v, --verbose
Provide more information (useful for debugging).
-w, --write-out <format>
Make curl display information on stdout after a completed transfer. See man page for more details on
available variables. Convenient way to force curl to append a newline to output: -w "\n"
(can add
to ~/.curlrc
).
-X, --request
The request method to use.
POST
When sending data via a POST or PUT request, two common formats (specified via the Content-Type
header) are:
application/json
application/x-www-form-urlencoded
Many APIs will accept both formats, so if you're using curl
at the command line, it can be a bit easier to use the form urlencoded format instead of json because
- the json format requires a bunch of extra quoting
- curl will send form urlencoded by default, so for json the
Content-Type
header must be explicitly set
This gist provides examples for using both formats, including how to use sample data files in either format with your curl
requests.
curl usage
For sending data with POST and PUT requests, these are common curl
options:
-
request type
-X POST
-X PUT
-
content type header
-
-H "Content-Type: application/x-www-form-urlencoded"
-
-H "Content-Type: application/json"
-
data
- form urlencoded:
-d "param1=value1¶m2=value2"
or-d @data.txt
- json:
-d '{"key1":"value1", "key2":"value2"}'
or-d @data.json
- form urlencoded:
Examples
POST application/x-www-form-urlencoded
application/x-www-form-urlencoded
is the default:
curl -d "param1=value1¶m2=value2" -X POST http://localhost:3000/data
explicit:
curl -d "param1=value1¶m2=value2" -H "Content-Type: application/x-www-form-urlencoded" -X POST http://localhost:3000/data
with a data file
curl -d "@data.txt" -X POST http://localhost:3000/data
POST application/json
curl -d '{"key1":"value1", "key2":"value2"}' -H "Content-Type: application/json" -X POST http://localhost:3000/data
with a data file
curl -d "@data.json" -X POST http://localhost:3000/data
©xgqfrms 2012-2020
www.cnblogs.com 发布文章使用:只允许注册用户才可以访问!
本文首发于博客园,作者:xgqfrms,原文链接:https://www.cnblogs.com/xgqfrms/p/12264835.html
未经授权禁止转载,违者必究!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· .NET10 - 预览版1新功能体验(一)