ducaale / xh

Friendly and fast tool for sending HTTP requests

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

`--stream` does not print entire buffer

devunt opened this issue · comments

I'm testing streaming SSR react server with xh, and I found that xh does not display entire response unlike what curl does.

Testing against same endpoint with xh and curl:

~ ❱ xh http://127.0.0.1:3000 --stream
HTTP/1.1 200 OK
Access-Control-Allow-Origin: *
Connection: keep-alive
Content-Type: text/html
Date: Sun, 04 Sep 2022 14:28:37 GMT
Keep-Alive: timeout=72
Transfer-Encoding: chunked

<!DOCTYPE html>
    <html>
      <head>
        <title>hello</title>
        <meta charset="utf-8" />
        <meta name="viewport" content="initial-scale=1, maximum-scale=1, width=device-width" />
      </head>
      <body>
~ ❱ curl http://127.0.0.1:3000 --include
HTTP/1.1 200 OK
Access-Control-Allow-Origin: *
Content-Type: text/html
Date: Sun, 04 Sep 2022 14:30:13 GMT
Connection: keep-alive
Keep-Alive: timeout=72
Transfer-Encoding: chunked

<!DOCTYPE html>
    <html>
      <head>
        <title>hello</title>
        <meta charset="utf-8" />
        <meta name="viewport" content="initial-scale=1, maximum-scale=1, width=device-width" />
      </head>
      <body>
~ ❱ curl http://127.0.0.1:3000 --include --no-buffer
HTTP/1.1 200 OK
Access-Control-Allow-Origin: *
Content-Type: text/html
Date: Sun, 04 Sep 2022 14:31:16 GMT
Connection: keep-alive
Keep-Alive: timeout=72
Transfer-Encoding: chunked

<!DOCTYPE html>
    <html>
      <head>
        <title>hello</title>
        <meta charset="utf-8" />
        <meta name="viewport" content="initial-scale=1, maximum-scale=1, width=device-width" />
      </head>
      <body>
        <div id="root"><!--$?--><template id="B:0"></template>suspending...<!--/$-->

As you can see, content of the body is printed only when curl is used with --no-buffer parameter.
I can't find xh option equivalent with curl --no-buffer from documentation or source code.

Any help would be appriciated!

This happens because --stream only shows complete lines, like HTTPie. See httpie/cli#1060

We don't have an option like --no-buffer. Maybe we should.

I see two workarounds:

  1. Send a newline immediately after <div id="root"><!--$?--><template id="B:0"></template>suspending...<!--/$-->. Then curl won't need --no-buffer either.

  2. Pipe the output through cat: xh :3000 --stream --print=hb | cat
    This disables the "smart" line-buffering and just sends everything through as soon as it's received.
    (--print=hb ensures you still see the headers, those are hidden by default when piping.)