How to get response time from curl request (via command-line) - performance

How can I capture the response time from a CURL request?
curl https://www.google.com

I found this approach in an article that returns a value in seconds
curl -o /dev/null -s -w %{time_total}\\n https://www.google.com
It outputs something like:
0.059

Related

Curl GET request is not giving any output in body

Via Postman the Api which i am using is:-
https://myCertManager.com/api/pik/restapi/MyCerts?INPUT_DATA={"operation":{"details"{"Cert_Name":"cert1"}}}
with authtoken in header "AUTHTOKEN: XXXX-XXXX-XXXX-XXXX"
When I send the get request via postman I get my certificate in the response body which is correct
Now I need to fetch this cert via curl command, so I created these two commands but when I execute them they connect to the server but the body of the response is coming empty.
curl -g -k -v -X GET https://myCertManager.com/api/pik/restapt/MyCerts?INPUT_DATA={"operation":{"details"{"Cert_Name":"cert1"}}} -H "AUTHTOKEN: XXXX-XXXX-XXXX-XXXX"
curl -k -v -X GET https://myCertManager.com/api/pik/restapt/MyCerts -H "AUTHTOKEN: XXXX-XXXX-XXXX-XXXX" -d "INPUT_DATA={"operation":{"details"{"Cert_Name":"cert1"}}}"
In postman if its working , you can generate equalent curl command by clicking the code button on the right to[ corner , it will create the curl code snipet for you
The issue was because the GET parameter doesn't support -d parameter. So this type of code will never work.
curl -k -v -X GET https://myCertManager.com/api/pik/restapt/MyCerts -H "AUTHTOKEN: XXXX-XXXX-XXXX-XXXX" -d "INPUT_DATA={"operation":{"details"{"Cert_Name":"cert1"}}}"
Secondly the GET parameter only sends the data through the url itself so the INPUT_DATA needs to be in the URL itself like this one
curl -g -k -v -X GET https://myCertManager.com/api/pik/restapt/MyCerts?INPUT_DATA={"operation":{"details"{"Cert_Name":"cert1"}}} -H "AUTHTOKEN: XXXX-XXXX-XXXX-XXXX"
But the URL dosent support the braces and quotes {} "" and to what i did was converted the INPUT_DATA to the URL format. So the actual working code is this one.
curl -g -k -v -X GET https://myCertManager.com/api/pik/restapt/MyCerts?INPUT_DATA=%7B%22operation%22%3A%7B%22details%22%7B%22Cert_Name%22%3A%22cert1%22%7D%7D%7D -H "AUTHTOKEN: XXXX-XXXX-XXXX-XXXX"

Bash curl call second command only when status is not 200

I looking for solution how combine two curl request in bash, and call second curl only when first doesnt return status 200.
I tried:
curl -s "https://example.com/first" || curl -s "https://example.com/second"
but it still call both because first curl is successful if return for example status 404.
How it is possible call second only when first doesnt return status 200?
Thanks for help.
curl -s -o /dev/null -w "%{http_code}" https://example.com | grep -q "^200$" || curl -s https://example.com/2.html
Edit: added improvement by #tripleee to not pollute output with grep output.

Stream cURL response to another cURL command posting the result

There's a public streaming Meetup API that streams JSON events: http://stream.meetup.com/2/rsvps
In a single line from the command line, I'd like to be able to redirect that stream such that each JSON object is POSTed to another API.
I've tried lots of permutations of cURL but thus far have been unable to make it work. My current attempt looks something like:
curl -s http://stream.meetup.com/2/rsvps | curl -H "Content-Type: application/json" -X POST -d #- 'http://my-api-url-here'
This produces nothing as far as I can see. The requests are not streamed to the destination API and nothing is sent to standard out. As a bonus, I'd like to see the standard output but the core requirement is streaming the JSON records to the target API.
If I try redirecting the stream to a file like:
curl -s http://stream.meetup.com/2/rsvps > output.txt
This works as expected. The challenge seems to be getting the output from the initial cURL stream to POST as data to the second.
This could work for let's say, 10 events at a time
curl -H "Content-Type: application/json" -X POST -d "[$(curl -s http://stream.meetup.com/2/rsvps | sed -n '1{p; :loop n; p; 10q; b loop}' | sed -re '1,9 s/[}]$/},/')]" 'http://my-api-url-here'
As suggested by meetup docs, the number of events may be limited using since_count and since_mtime.

How to redirect two output of two different commands in Linux to separate file?

The result of curl -s http://127.0.0.1 is 200 OK
The result of /usr/bin/time -f "%e" curl -s http://127.0.0.1 is 200 OK0.08
In this case, I only need 0.08. How can I only get 0.08 instead of the whole string when I redirect the output to >> result.txt?
ps: the response can change in the future (not always 200 OK), and the format of time can also change in the future. So I really need a solution that is not based on string manipulation. Thanks.
Do this:
/usr/bin/time -f "%e" curl -s -o /dev/null http://127.0.0.1
By adding -o /dev/null you are telling curl to write its output nowhere, leaving you with only the output of time.

I need to execute a Curl script on Jenkins so as to check the status URL

The script should check for Http status code for the URL and should show error when status code doesn't match for eg. 200.
In Jenkins if this script fails then Build should get failed and Mail is triggered through post build Procedure.
Another interesting feature of curl is its -f/--fail option. If set, it will tell curl to fail on any HTTP error, i.e. curl will have an exit code different from 0, if the server response status code was not 1xx/2xx/3xx, i.e. if it was 4xx or above, so
curl --silent --fail "http://www.example.org/" >/dev/null
or (equivalently):
curl -sf "http://www.example.org/" >/dev/null
would have an exit code of 22 rather than 0, if the URL could not be found or if some other HTTP error occurred. See man curl for a description of curl's various exit codes.
You can use simple shell command as referred in this answer
curl -s -o /dev/null -w "%{http_code}" http://www.example.org/
This will happen if the following shell script is added:
response=$(curl -s -o /dev/null -w "%{http_code}\n" http://www.example.org/)
if [ "$response" != "200" ]
then
exit 1
fi
exit 1 will mark build as failed
Jenkins also has HTTP Request Plugin that can trigger HTTP requests.
For example this is how you can check response status and content:
def response = httpRequest "http://httpbin.org/response-headers?param1=${param1}"
println('Status: '+response.status)
println('Response: '+response.content)
You could try:
response=`curl -k -s -X GET --url "<url_of_the_request>"`
echo "${response}"
How about passing the URL at run time using curl in bashscript
URL=www.google.com
"curl --location --request GET URL"
How we can pass url at runtime ?

Resources