cURL - Structuring request to validate server sent events

前端 未结 2 1341
庸人自扰
庸人自扰 2020-12-10 12:07

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 eve

2条回答
  •  一生所求
    2020-12-10 12:52

    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 -N http://127.0.0.1/path/to/clock.php
    

    (The -N stops any buffering, so data is shown as it is received.)

    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 -N --cookie cookies.txt http://127.0.0.1/path/to/clock.php
    

    See other answer and the curl documentation for other options you might want to consider using. If you are troubleshooting problems in a specific browser, use their devtools to find out exactly what headers are being sent, and then you can tell curl up to do the same.


    For completeness, here is the clock.php script:

提交回复
热议问题