system call to curl doesnt shows output from a ruby script - ruby

I have a ruby script:
#!/usr/bin/env ruby
`curl -X GET http://host/someurl'
The response doesnt get displayed on terminal when I run this script:
$ ./script.rb
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 146 0 146 0 0 73 0 --:--:-- 0:00:01 --:--:-- 73
$
The server does send some data. If I supply -o to curl:
`curl -X GET -o <some_file> http://host/someurl'
some_file contains server response. Same works for POST requests though:
`curl -X POST --data-binary #some_file http://host/someurl'
This shows the response on terminal. Any idea how I fix this?

just puts it
puts `curl -X GET http://host/someurl`

You have a typo closing your back-tick shell command. You want:
`curl -X GET http://host/someurl`
You used a single quotation mark instead of a trailing back-tick. This causes the expression not to be terminated.

Related

Show progress bar in curl without showing response content

Is there are way within Bash to hide the response body of a curl GET request whilst still showing a progress bar?
--progress-bar Shows a minified version of the default bar however still outputs everything.
Likewise with --silent which hides everything which is unhelpful for the volume of requests.
My current code is,
for i in $(cat scripts/urls.txt); do
file="scripts/output.txt"
content=$(curl --location --request GET $i -H 'Authorization: ...')
"$content" >> $file
cat $file
sleep 5
done
The progress bar is sent to stderr. The response is sent to stdout. To redirect and append the response to a file you could use the >> operator:
while read -r i; do
file="scripts/output.txt"
curl --location --request GET $i -H 'Authorization: ...' >> $file
sleep 5
done < scripts/urls.txt
$ curl -X GET http://www.example.com > /dev/null
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 1256 100 1256 0 0 22445 0 --:--:-- --:--:-- --:--:-- 22836

Install script curl'ed from github:

I have the following script hosted on Github:
https://rawgit.com/oresoftware/quicklock/master/install.sh
the contents of that file are:
#!/usr/bin/env bash
set -e;
cd "$HOME"
mkdir -p "$HOME/.quicklock/locks"
curl https://rawgit.com/oresoftware/quicklock/master/install.sh > "$HOME/.quicklock/ql.sh"
echo "To complete installation of 'quicklock' add the following line to your .bash_profile file:";
echo ". \"$HOME/.quicklock/ql.sh\"";
I download and run this script with:
curl -o- https://rawgit.com/oresoftware/quicklock/master/install.sh | bash
but I get this error:
bash: line 1: Moved: command not found
That error is killing me, I cannot figure out what is causing it. I tried curl with both the -o- option and without.
The url for raw git has changed, the error itsel is from curl.
Change rawgit.com to raw.githubusercontent.com.
Another option is to add -L to have curl follow the redirect link.
I figured this out by changing bash to bash -x. Here is the output:
curl -o- https://rawgit.com/oresoftware/quicklock/master/install.sh | bash -x
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 107 100 107 0 0 400 0 --:--:-- --:--:-- --:--:-- 402
+(:1): Moved Permanently. Redirecting to https://raw.githubusercontent.com/oresoftware/quicklock/master/install.sh
bash: line 1: Moved: command not found
#xxfelixxx is pretty much right
This was sort of nightmare, but there appears to be a redirect even when using raw.githubusercontent.com
the only thing that worked with curl was to use:
curl -o- https://raw.githubusercontent.com/oresoftware/quicklock/master/install.sh | bash
For the scripts that require arguments, you can do _ for the script placeholder and then the arguments. For exampe: example.sh that expects --help
curl -L https://raw.githubusercontent.com/<USER>/<NAME>/<BRANCH>/example.sh | bash -s _ --help

How to tidy up a curl output and make it throw errors inside of Jenkins job?

I use curl commands in my jenkins jobs and I have some questions about them. I have a command like this. Here a get a json object, parse it and put the result to array "fileslist"
fileslist=($(curl -u user:password -X POST -k http://server:8081/artifactory/api/search/aql -d "items.find({\"type\" : \"folder\", \"repo\" : \"${REPOSITORY}\", \"path\" : \".\", \"name\" : {\"\$nmatch\" : \"\.\"}, \"modified\" : {\"\$lt\" : \"$borderdate\"} })" | jq --raw-output '.results | .[] | .name'))
When I use it, I have this information in my output , that I don't really want to see.
.
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
0 0 0 0 0 0 0 0 --:--:-- --:--:-- --:--:-- 0
100 1196 100 1052 100 144 227k 31858 --:--:-- --:--:-- --:--:-- 1027k
I can use --silent option, but it makes silent all the output, so it doesn't match with --fail option. What I need is to see in my log only information about errors, that occurred during the curl command.
I want the command above (if something goes wrong) write a error and STOP a Jenkins job. When I use --fail option, it just writes to the output about a error and my script continues. You can tell me to use Parse Input Plugin, but it is not what I want: It marks a build as "failed build" AFTER the script finished it's work, but I want to make it stop immediately.
The same questions about wget command.
Thank you in advance!

Reduce output of curl command

When I run a curl command against my non-running server, I get below output.
I only want
Failed to connect: Connection Refused
How can I remove the extra output. If I run --silent, it suppress everything.
I get this o/p for curl http://localhost:300/index.html
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
0 0 0 0 0 0 0 0 --:--:-- --:--:-- --:--:-- 0
100 9626 100 9626 0 0 148k 0 --:--:-- --:--:-- --:--:-- 149k
Failed to connect: Connection Refused
yes! using -s, --silent along with -S, --show-error is the right option.
Curl gives me the compressed output by default, but try:
curl --compressed http://localhost:300/index.html
By contrast --verbose returns more lengthy output.
man curl will show you all the options available to you.
Looks like I need to use -sS. man curl will show you all the options available to you.

How to use awk to parse urls and pass to curl to download it?

I want to download a binary from https://github.com/sschwartzman/newrelic-unix-plugin. but the url will redirect to another address, so I use the awk to parse it. e.g.
curl -I https://github.com/sschwartzman/newrelic-unix-plugin/blob/master/dist/newrelic_unix_plugin.tar.gz\?raw\=true | awk '/Location:/ {print $2}'
And the result is as I expected, e.g.
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
0 0 0 0 0 0 0 0 --:--:-- 0:00:01 --:--:-- 0
https://github.com/sschwartzman/newrelic-unix-plugin/raw/master/dist/newrelic_unix_plugin.tar.gz
The real address is parsed. so I just want to use curl to download it. but I always got fail.
My os is osx El Capitan (I don't have wget).
curl -I https://github.com/sschwartzman/newrelic-unix-plugin/blob/master/dist/newrelic_unix_plugin.tar.gz\?raw\=true | awk '/Location:/ {curl -O $2}'
p.s.
I tried to download the binary directly by curl, but still failed. The downloaded the file was incorrect size and content.
curl -O https://github.com/sschwartzman/newrelic-unix-plugin/raw/master/dist/newrelic_unix_plugin.tar.gz
curl has switch -L which can handle redirect.
So, this should work:
curl -L https://github.com/sschwartzman/newrelic-unix-plugin/blob/master/dist/newrelic_unix_plugin.tar.gz\?raw\=true -o newrelic_unix_plugin.tar.gz

Resources