cURL - 结构化请求以验证服务器发送的事件 [英] cURL - Structuring request to validate server sent events
问题描述
我是新到cURL和服务器发送的事件。我知道如何构建一个简单的GET,POST请求使用cURL和获得响应。另外,理论上我知道服务器发送的事件是通过创建事件源的侦听器来处理,但我不知道如何继续使用cURL验证任何这样的API。任何指导是赞赏。
SSE是一种基于文本的协议,curl是一种很好的方法来排除您的连接发送的问题。命令很简单:
curl http://127.0.0.1/path/to/clock.php
并输出:
数据:2015-07-07 06:19:27
数据:2015-07-07 06:19:28
数据:2015-07- 07 06:19:29
资料:2015-07-07 06:19:30
资料:2015-07-07 06:19:31
data:2015-07-07 06:19:32
data:前缀的SSE协议,并且还清楚地显示双LF。它运行永远,直到你按ctrl-c。
关于唯一要指出的是,你必须使用一个Web服务器;您不能通过文件://协议运行SSE。
对于更多硬核故障排除,请添加 - verbose
SSE支持cookies,您可以这样提供:(您必须首先准备好cookies.txt文件):
curl --cookie cookies.txt http://127.0.0.1/path/到/ clock.php
为了完整起见, clock.php脚本:
<?php
set_time_limit
header(Content-type:text / event-stream);
while(1){
echodata:。日期(Y-m-d H:i:s)。 \\\
\\\
;
@ob_flush(); flush();
sleep(1);
}
I am new to cURL and server sent events. I know how to build a simple GET, POST requests using cURL and getting response. Also, Theoretically I am aware that server sent events are handled by creating a listener to event source but I am not sure how to proceed with validating any such API with cURL. Any guidance is appreciated.
解决方案SSE is a text-based protocol, and curl is a great way to troubleshoot exactly what your connection is sending. The command is this simple:
curl http://127.0.0.1/path/to/clock.php
And it outputs this:
data:2015-07-07 06:19:27 data:2015-07-07 06:19:28 data:2015-07-07 06:19:29 data:2015-07-07 06:19:30 data:2015-07-07 06:19:31 data:2015-07-07 06:19:32
Notice how it shows the "data:" prefix of the SSE protocol, and also clearly shows the double LFs. It runs forever, until you press ctrl-c.
About the only thing to point out is that you must use a web server; you cannot run SSE over the file:// protocol.
For more hard-core troubleshooting, add
--verbose
, which will show the headers being sent, and the headers being received.SSE does support cookies, which you could give like this: (you would first have to prepare the "cookies.txt" file):
curl --cookie cookies.txt http://127.0.0.1/path/to/clock.php
For completeness, here is the clock.php script:
<?php set_time_limit(0); header("Content-type: text/event-stream"); while(1){ echo "data:" . date("Y-m-d H:i:s") . "\n\n"; @ob_flush();flush(); sleep(1); }
这篇关于cURL - 结构化请求以验证服务器发送的事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!