如何在 bash 中执行 HTTP PUT?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8467096/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 01:09:58  来源:igfitidea点击:

How do I execute an HTTP PUT in bash?

bashput

提问by John

I'm sending requests to a third-party API. It says I must send an HTTP PUT to http://example.com/project?id=projectId

我正在向第三方 API 发送请求。它说我必须发送一个 HTTP PUT 到http://example.com/project?id=projectId

I tried doing this with PHP curl, but I'm not getting a response from the server. Maybe something is wrong with my code because I've never used PUT before. Is there a way for me to execute an HTTP PUT from bash command line? If so, what is the command?

我尝试使用 PHP curl 执行此操作,但没有收到服务器的响应。也许我的代码有问题,因为我以前从未使用过 PUT。有没有办法让我从 bash 命令行执行 HTTP PUT?如果是这样,命令是什么?

采纳答案by Arnon Rotem-Gal-Oz

With curl it would be something like

使用 curl 它会是这样的

curl --request PUT --header "Content-Length: 0" http://website.com/project?id=1

but like Mattias said you'd probably want some data in the body as well so you'd want the content-type and the data as well (plus content-length would be larger)

但就像马蒂亚斯所说的那样,您可能还需要正文中的一些数据,因此您还需要内容类型和数据(加上内容长度会更大)

回答by Mattias Wadman

If you really want to only use bash it actually has some networking support.

如果你真的只想使用 bash,它实际上有一些网络支持。

 echo -e "PUT /project?id=123 HTTP/1.1\r\nHost: website.com\r\n\r\n" > \
   /dev/tcp/website.com/80

But I guess you also want to send some data in the body?

但是我猜你也想在正文中发送一些数据?