Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

http doesnt work within a bash loop #181

Closed
filipesperandio opened this issue Nov 23, 2013 · 5 comments
Closed

http doesnt work within a bash loop #181

filipesperandio opened this issue Nov 23, 2013 · 5 comments

Comments

@filipesperandio
Copy link

Looks like a leackage.

I have this file, links.txt:
www.google.com?q=1  
www.google.com?q=2  
www.google.com?q=3  
www.google.com?q=4  


Running this:
$ cat links.txt | while read line; do http --print BHhb GET $line; done

Expected result:
4 http calls, one for each line on the file.

Actual:
1 request executed to the first url in the file and request body having the other 3 lines.

GET /?q=1 HTTP/1.1
Accept: application/json
Accept-Encoding: gzip, deflate, compress
Content-Length: 63
Content-Type: application/json; charset=utf-8
Host: www.google.com
User-Agent: HTTPie/0.6.0

www.google.com?q=2
www.google.com?q=3
www.google.com?q=4

HTTP/1.0 400 Bad Request
Content-Length: 925
Content-Type: text/html; charset=UTF-8
Date: Sat, 23 Nov 2013 19:48:51 GMT
Server: GFE/2.0

<!DOCTYPE html>
<html lang=en>
  <meta charset=utf-8>
  <meta name=viewport content="initial-scale=1, minimum-scale=1, width=device-width">
  <title>Error 400 (Bad Request)!!1</title>
  <style>
    *{margin:0;padding:0}html,code{font:15px/22px arial,sans-serif}html{background:#fff;color:#222;padding:15px}body{margin:7% auto 0;max-width:390px;min-height:180px;padding:30px 0 15px}* > body{background:url(//www.google.com/images/errors/robot.png) 100% 5px no-repeat;padding-right:205px}p{margin:11px 0 22px;overflow:hidden}ins{color:#777;text-decoration:none}a img{border:0}@media screen and (max-width:772px){body{background:none;margin-top:0;max-width:none;padding-right:0}}
  </style>
  <a href=//www.google.com/><img src=//www.google.com/images/errors/logo_sm.gif alt=Google></a>
  <p><b>400.</b> <ins>That’s an error.</ins>
  <p>Your client has issued a malformed or illegal request.  <ins>That’s all we know.</ins>



Running same script with curl instead works as expected.


cheers!

@jkbrzt
Copy link
Member

jkbrzt commented Nov 23, 2013

It's because HTTPie by default reads STDIN, if it is redirected (which it is here inside the loop).

There are multiple ways to get rid of it:

  • The best way is to use --ignore-stdin (which exists to solve exactly this problem):
cat links.txt | while read url; do 
        http --ignore-stdin --verbose $url
done
  • Or, change STDIN back to terminal:
cat links.txt | while read url; do
        http --verbose $url < /dev/tty
done
  • Or, do not redirect STDIN in the first place (watch out for spaces though):
for url in `cat links.txt`; do 
        http --verbose $url
done

See also #150.

@jkbrzt jkbrzt closed this as completed Nov 23, 2013
@filipesperandio
Copy link
Author

The --ignore-stdin is what I was looking for. I had to update the installed
version, but seems to be working as expected now.
I love httpie, but this little thing got me sometime to figure...

Thanks for the very fast response.
Cheers!

On Sat, Nov 23, 2013 at 4:17 PM, Jakub Roztočil notifications@github.comwrote:

Closed #181 #181.


Reply to this email directly or view it on GitHubhttps://github.com//issues/181
.

Filipe Gomes Esperandio

Gtalk: fiilipesperandio@gmail.com
Skype: filipesperandio
Google Voice: +1 650 701 7057

@jezus-nunez
Copy link

jezus-nunez commented Apr 9, 2019

Hi, I had a similar issue which was solved via --ignore-stdin but my context is a bit different:

./run.sh http https://base-url/endpoint\?attributes=all\&nested_attribbutes\=all Authorization:Bearer\ bf54b184d7729ac9bfffb576782e2a1d3cd7bd76 Content-Type:application/json Accept:\ \*/\* -h

I was getting HTTP/1.1 422 Unprocessable Entity

The run.sh script looks as follows: seq 5 | xargs -I {} -n1 "$@"

Adding --ignore-stdin fixes the issue, but I am not reading the request from STDIN. Could you please explain me how it works in this context?

@Frankz
Copy link

Frankz commented Feb 23, 2021

It's because HTTPie by default reads STDIN, if it is redirected (which it is here inside the loop).

There are multiple ways to get rid of it:

  • The best way is to use --ignore-stdin (which exists to solve exactly this problem):
cat links.txt | while read url; do 
        http --ignore-stdin --verbose $url
done
  • Or, change STDIN back to terminal:
cat links.txt | while read url; do
        http --verbose $url < /dev/tty
done
  • Or, do not redirect STDIN in the first place (watch out for spaces though):
for url in `cat links.txt`; do 
        http --verbose $url
done

See also #150.

Thank you very much

@adriens
Copy link

adriens commented Jan 29, 2023

Thanks a lot for the --ignore-stdin tip 🙏

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants