如何用curl做PUT请求?

本文翻译自:How to do a PUT request with curl?

如何使用curl测试RESTful PUT(或DELETE)方法?


#1楼

参考:https://stackoom.com/question/vpNW/如何用curl做PUT请求


#2楼

Using the -X flag with whatever HTTP verb you want: -X标志与您想要的任何HTTP动词一起使用:

curl -X PUT -d arg=val -d arg2=val2 localhost:8080

This example also uses the -d flag to provide arguments with your PUT request. 此示例还使用-d标志为您的PUT请求提供参数。


#3楼

An example PUT following Martin C. Martin's comment: 以马丁C.马丁评论为例的PUT示例:

curl -T filename.txt http://www.example.com/dir/

With -T (same as --upload-file ) curl will use PUT for HTTP. 使用-T (与--upload-file相同)curl将使用PUT进行HTTP。


#4楼

You can use the POSTMAN app from Chrome Store. 您可以使用Chrome Store中的POSTMAN应用。

In a single line, the curl command would be: 在一行中,curl命令将是:

a) If sending form data: a)如果发送表格数据:

curl -X PUT -H "Content-Type: multipart/form-data;" -F "key1=val1" "YOUR_URI"

b) If sending raw data as json: b)如果以json发送原始数据:

curl -X PUT -H "Content-Type: application/json" -d '{"key1":"value"}' "YOUR_URI"

c) If sending a file with a POST request: c)如果发送带有POST请求的文件:

curl -X POST "YOUR_URI" -F 'file=@/file-path.csv'

For the request with other formats or for different clients like java, PHP, you can check out POSTMAN/comment below. 对于其他格式的请求或java,PHP等不同客户端的请求,您可以查看下面的POSTMAN /评论。

POSTMAN获取请求代码


#5楼

curl -X PUT -d 'new_value' URL_PATH/key

where, 哪里,

X - option to be used for request command X - 用于请求命令的选项

d - option to be used in order to put data on remote url d - 用于将数据放在远程URL上的选项

URL_PATH - remote url URL_PATH - 远程网址

new_value - value which we want to put to the server's key new_value - 我们想要放入服务器密钥的值


#6楼

I am late to this thread, but I too had a similar requirement. 我迟到了这个帖子,但我也有类似的要求。 Since my script was constructing the request for curl dynamically, I wanted a similar structure of the command across GET, POST and PUT. 由于我的脚本是动态构建curl的请求,所以我希望在GET,POST和PUT之间有一个类似的命令结构。

Here is what works for me 这对我有用

For PUT request: 对于PUT请求:

curl --request PUT --url http://localhost:8080/put --header 'content-type: application/x-www-form-urlencoded' --data 'bar=baz&foo=foo1'

For POST request: 对于POST请求:

curl --request POST --url http://localhost:8080/post --header 'content-type: application/x-www-form-urlencoded' --data 'bar=baz&foo=foo1'

For GET request: 对于GET请求:

curl --request GET --url 'http://localhost:8080/get?foo=bar&foz=baz'
  • 2
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
回答: 对于使用curl进行PUT请求,可以使用以下代码示例: ```php function curl2($url,$data = '', $https = true) { $ch = curl_init(); $header=array( "Content-Type: application/json;charset=utf-8", ); curl_setopt($ch, CURLOPT_URL, $url); //定义请求地址 curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);//定义是否直接输出返回流 curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT'); //定义请求类型,必须为大写 curl_setopt($ch, CURLOPT_HTTPHEADER, $header);//定义header curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data)); //定义提交的数据 curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);//这个是重点。 $res = curl_exec($ch); curl_close($ch);//关闭 return $res; // 返回数据 } ``` 这段代码定义了一个名为curl2的函数,用于发送PUT请求。你可以将请求的URL和数据作为参数传递给该函数。在函数内部,使用curl_setopt函数设置了请求的URL、请求类型、请求头、提交的数据等参数。最后,使用curl_exec函数执行请求并返回结果。 #### 引用[.reference_title] - *1* *3* [如何用curlPUT请求?](https://blog.csdn.net/CHCH998/article/details/106489345)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [curl put请求](https://blog.csdn.net/RemixGdc/article/details/79258129)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值