0

I'm trying to analyze this command:

$ http :"/hello"
HTTP/1.1 401 Unauthorized
<headers>

<body>

I'm trying to save the whole thing in a variable VAR=$( ... ) but to no avail so far.

If I run

$ http :"/hello" 1>/dev/null

everything disappear, so I derive that everything is standard output.

But if I try to send this to a file or to my variable, I don't see the initial portion. So I thought this was stderr, so I did 2>&1 but this doesn't have any effect either.

How can I go about understanding this?

Thanks

JoeSlav
  • 4,479
  • 4
  • 31
  • 50
  • What is `http`? It may be altering its output, based on whether its standard output is a terminal or not. – chepner May 25 '21 at 13:15
  • It's from this tool: https://httpie.io/ – vladmihaisima May 25 '21 at 13:16
  • Did you try `VAR=$( http :"/hello" )`? `so I did 2>&1` Please post the exact code that you have tried! did you do `VAR=$(...) 2>&1` or `VAR=$(... 2>&1 )`? Please prefer showing what you did. – KamilCuk May 25 '21 at 14:42

2 Answers2

1

HTTPie alters its output, depending on whether it is writing to the terminal or a regular file.

From the manual

Redirected output

HTTPie uses a different set of defaults for redirected output than for terminal output. The differences being:

Formatting and colors aren’t applied (unless --pretty is specified). Only the response body is printed (unless one of the output options is set). Also, binary data isn’t suppressed.

[...]

There are specific command line options you can use to override the defaults.

chepner
  • 497,756
  • 71
  • 530
  • 681
0

You could try using option "-v" and "-o" to capture both headers and body in a file, as in:

http -v -o output.file "google.com/bla"

As mentioned by @chepner in the comment, some tools detect if stdout is a file or not and change what they output.

Edit: to work easiest as you wanted (to stdout) you can use:

http -v -o /dev/stdout "google.com/bla"
vladmihaisima
  • 2,119
  • 16
  • 20
  • Such a hackish way to do stuff! This tool is terrible. I will try to make it work with -v. Thanks! – JoeSlav May 25 '21 at 13:56
  • If you ask a question about a specific tool, the assumption is that you decided to use it. You could use alternatives (wget, curl come to mind), maybe they are better suited for your needs. – vladmihaisima May 25 '21 at 14:08
  • I wish I could man... I wish I could... I'm stuck with this.. – JoeSlav May 25 '21 at 14:09