首页 > 编程知识 正文

curl指定请求方式,put请求干什么用的

时间:2023-05-06 20:30:03 阅读:274242 作者:4351

本文翻译自: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 /评论。

#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'

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。