Trevor Sullivan
Trevor Sullivan•2y ago

Getting Empty Output from Bash Script

I have a painfully simple Bash script that retrieves data from a publicly (anonymously) accessible REST API. Sometimes the script outputs the expected JSON, but the vast majority of the time, I'm not getting the JSON output from the curl command. What could possibly be happening? If I run this in a Linux shell, it works every single time.
54 Replies
Trevor Sullivan
Trevor SullivanOP•2y ago
The same command works perfectly fine in a Linux shell.
rubenf
rubenf•2y ago
Let me investigate
Trevor Sullivan
Trevor SullivanOP•2y ago
I'm trying Python with httpx instead. Maybe it's more reliable that way. Was hoping to use Bash script for simplicity though
Trevor Sullivan
Trevor SullivanOP•2y ago
Python works perfectly, on the first try
rubenf
rubenf•2y ago
bash relies on stdout for result while python works through file
Trevor Sullivan
Trevor SullivanOP•2y ago
Let me try using httpie from Bash script
rubenf
rubenf•2y ago
it's just a race condition on stdout
Trevor Sullivan
Trevor SullivanOP•2y ago
sleep 1 after curl 😆
rubenf
rubenf•2y ago
I can reproduce but this is really dumbfounding me it's not just a race condition
Trevor Sullivan
Trevor SullivanOP•2y ago
I found another odd behavior.
Trevor Sullivan
Trevor SullivanOP•2y ago
Trevor Sullivan
Trevor SullivanOP•2y ago
It works in Linux shell, but "not found" in Windmill
rubenf
rubenf•2y ago
name is empty in your example
Trevor Sullivan
Trevor SullivanOP•2y ago
Oops
rubenf
rubenf•2y ago
oh you mean pip3 install
Trevor Sullivan
Trevor SullivanOP•2y ago
Trevor Sullivan
Trevor SullivanOP•2y ago
Still the same But the Python example, with httpx works just fine
rubenf
rubenf•2y ago
it's really weird
Trevor Sullivan
Trevor SullivanOP•2y ago
It's not a problem finding the http binary .... because I can do http --version and it prints the version
Trevor Sullivan
Trevor SullivanOP•2y ago
See, it prints the version, but not the output from the API call
rubenf
rubenf•2y ago
if one pipe with jq it works everytime curl --silent https://api.nationalize.io?name=michael echo "" this will work everytime
Trevor Sullivan
Trevor SullivanOP•2y ago
Weird ...
rubenf
rubenf•2y ago
it looks like the buffer is not getting drained but I think that's a bash thing
Trevor Sullivan
Trevor SullivanOP•2y ago
Yup, confirmed. That fixes the issue with curl. But with httpie I still just get "not found" Maybe separate issues
rubenf
rubenf•2y ago
definitely separate
Trevor Sullivan
Trevor SullivanOP•2y ago
Seems like a variable expansion issue in Bash. I hard-coded a value and http works.
Trevor Sullivan
Trevor SullivanOP•2y ago
Sorry, that was coming from curl. Not HTTP. Hard-coding value still fails
rubenf
rubenf•2y ago
does this command work on linux ?
Trevor Sullivan
Trevor SullivanOP•2y ago
Trevor Sullivan
Trevor SullivanOP•2y ago
Yup
rubenf
rubenf•2y ago
i committed a fix that will avoid the need for echo ""
Trevor Sullivan
Trevor SullivanOP•2y ago
Do you want a new bug report for the "not found" issue?
rubenf
rubenf•2y ago
I'm on it will probably solve it soon
rubenf
rubenf•2y ago
rubenf
rubenf•2y ago
I cannot reproduce
Trevor Sullivan
Trevor SullivanOP•2y ago
Hmm... Trying it another way, using a Docker container
rubenf
rubenf•2y ago
note that i'm having the echo "" baked in as part of my new fix
Trevor Sullivan
Trevor SullivanOP•2y ago
Try this now ....
rubenf
rubenf•2y ago
?
Trevor Sullivan
Trevor SullivanOP•2y ago
Calling API with PowerShell instead of http
Trevor Sullivan
Trevor SullivanOP•2y ago
Worked fine, first try
rubenf
rubenf•2y ago
lol
Trevor Sullivan
Trevor SullivanOP•2y ago
Maybe something odd with the http utility ... really odd though. I'm not doing any HTTP proxying
rubenf
rubenf•2y ago
Not found is really really weird can you do -v or something ?
Trevor Sullivan
Trevor SullivanOP•2y ago
Yeah, that's what I thought. I could expect many other errors, but not taht
Trevor Sullivan
Trevor SullivanOP•2y ago
Trevor Sullivan
Trevor SullivanOP•2y ago
http --verbose "https://api.nationalize.io?name=Trevor" That's .... bizarre? It's literally the same URL They must be doing some server-side check on the User-Agent header? But it works in Linux shell .....
rubenf
rubenf•2y ago
user agent would be the same
Trevor Sullivan
Trevor SullivanOP•2y ago
Oh well, I have plenty of alternatives. I'll just use PowerShell or Python, or curl in Bash Yeah, just thought of that after I posted it Let me do --verbose from Linux shell
Trevor Sullivan
Trevor SullivanOP•2y ago
Trevor Sullivan
Trevor SullivanOP•2y ago
Older version of HTTPie Whatever, I'm not worried about it. At least you verified it's not an issue on your side.
rubenf
rubenf•2y ago
GitHub
GitHub - PowerShell/PowerShell-Docker: Repository for building Powe...
Repository for building PowerShell Docker images. Contribute to PowerShell/PowerShell-Docker development by creating an account on GitHub.
rubenf
rubenf•2y ago
220MB just for powershell...
Trevor Sullivan
Trevor SullivanOP•2y ago
Yup, but it saves tons of development time

Did you find this page helpful?