bash 成功 POST 后 curl 不会终止

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/45737868/
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 16:22:32  来源:igfitidea点击:

curl does not terminate after successful POST

bashcurl

提问by Nils Rehwald

I have created some curl command to send a POST to my server where I am listening on that port for input to trigger additional action. The command is the following (Just masked the URL):

我创建了一些 curl 命令来将 POST 发送到我的服务器,我在该服务器上侦听该端口以获取输入以触发其他操作。命令如下(只是屏蔽了 URL):

curl -v -H "Content-Type: application/json" -X POST -d "{\"Location\":\"Some Name\",\"Value\":\"40%\"}" http://example.com:8885/

I get the following output from curl:

我从 curl 得到以下输出:

About to connect() to example.com port 8885 (#0)

Trying 5.147.XXX.XXX...

Connected to example.com (5.147.XXX.XXX) port 8885 (#0)

POST / HTTP/1.1

User-Agent: curl/7.29.0

Host: example.com:8885

Accept: /

Content-Type: application/json

Content-Length: 40

upload completely sent off: 40 out of 40 bytes

即将 connect() 到 example.com 端口 8885 (#0)

正在尝试 5.147.XXX.XXX...

连接到 example.com (5.147.XXX.XXX) 端口 8885 (#0)

POST / HTTP/1.1

用户代理:curl/7.29.0

主机:example.com:8885

接受:/

内容类型:应用程序/json

内容长度:40

上传完全发送:40 个字节中的 40 个

However after that curl does not close the connection. Am I doing something wrong? Also on the server I only receive the POST as soon as I hit ctrl+c.

但是,在那之后 curl 不会关闭连接。难道我做错了什么?同样在服务器上,我只在按 ctrl+c 后立即收到 POST。

回答by Daniel Stenberg

It sits there waiting for the proper HTTP response, and after that has been received it will exit cleanly.

它坐在那里等待正确的 HTTP 响应,在收到响应之后,它将干净地退出。

A minimal HTTP/1.1 response could look something like:

最小的 HTTP/1.1 响应可能类似于:

HTTP/1.1 200 OK
Content-Length: 0

... and it needs an extra CRLF after the last header to signal the end of headers.

...并且在最后一个标头之后需要一个额外的 CRLF 来表示标头结束。

回答by Polentino

I'm a bit rusty on this, but according to section 6.1 of RFC7230, you might need to add a Connection: closeheader as well. Quoting part of the paragraph:

我对此有点生疏,但根据RFC7230 的第 6.1 节,您可能还需要添加Connection: close标头。引用部分段落:

The "close" connection option is defined for a sender to signal that this connection will be closed after completion of the response. For example,

 Connection: close

in either the request or the response header fields indicates that the sender is going to close the connection after the current
request/response is complete (Section 6.6).

“关闭”连接选项是为发送者定义的,用于在响应完成后发出此连接将被关闭的信号。例如,

 Connection: close

在请求或响应头字段中,表明发送方将在当前
请求/响应完成后关闭连接(第 6.6 节)。

Let me know if it solves your issue :-)

让我知道它是否解决了您的问题:-)