httpie / cli

🥧 HTTPie CLI — modern, user-friendly command-line HTTP client for the API era. JSON support, colors, sessions, downloads, plugins & more.
https://httpie.io
BSD 3-Clause "New" or "Revised" License
32.74k stars 3.68k forks source link

Questions about http waiting for stdin #1460

Closed zxt5 closed 1 year ago

zxt5 commented 1 year ago

I am trying to use httpie from a Java program by using ProcessBuilder to run external commands.

When I run "http -h google.com", the process hangs since it's waiting for stdin. But if I run the same command in a termial instead of in a jave program, it won't hang but print the output and exit.

I know it can be fixed by using --ignore-stdin, but I am wondering why it does not hang when running in the terminal. What's the differences between these two situations?

jkbrzt commented 1 year ago

Hey @zxt539, here's an explanation from the docs:

What happens is that when HTTPie is invoked, for example, from a cron job, stdin is not connected to a terminal.

Essentially, stdin is a file descriptor, which may or may not point to a terminal—depending on the context in which HTTPie runs. If it’s not connected to a terminal, we try to read its contents because you might be piping in data (e.g., echo hello | http pie.dev/post).

Under the hood, we call sys.stdin.isatty() to find out:

Return True if the file descriptor fd is open and connected to a tty(-like) device, else False.

You can test it by running the following command in different contexts (from the terminal, from java, …):

#  In the terminal it’s True
$ python3 -c 'import sys;print(sys.stdin.isatty())'
True

# …unless it’s redirected
$ echo hello | python3 -c 'import sys;print(sys.stdin.isatty())'
False

Here’s more on the TTY concept: http://www.linusakesson.net/programming/tty/

zxt5 commented 1 year ago

Got it! Thanks for your reply!