Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

cURL output to file

Is it possible to direct this output from cURL into a file?

  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100  1354  100  1354    0     0  17358      0 --:--:-- --:--:-- --:--:-- 17358
100 67081  100 67081    0     0  68171      0 --:--:-- --:--:-- --:--:-- 4031k

I cannot find anything in --help that would indicate you can. -o just done the response from what I can tell.

I am just wanting to know if the request succeded and how long it took.

like image 787
ojhawkins Avatar asked Jan 20 '14 05:01

ojhawkins


People also ask

How do I copy curl output to a file?

For those of you want to copy the cURL output in the clipboard instead of outputting to a file, you can use pbcopy by using the pipe | after the cURL command. Example: curl https://www.google.com/robots.txt | pbcopy . This will copy all the content from the given URL to your clipboard.

How do you save curls?

To download a file with Curl, use the --output or -o command-line option. This option allows you to save the downloaded file to a local drive under the specified name. If you want the uploaded file to be saved under the same name as in the URL, use the --remote-name or -O command line option.

Can you download a file with curl?

curl lets you quickly download files from a remote system. curl supports many different protocols and can also make more complex web requests, including interacting with remote APIs to send and receive data.

What is the output of curl?

Use "-C -" to tell curl to automatically find out where/how to resume the transfer. It then uses the given output/input files to figure that out. If this option is used several times, the last one will be used. When used in conjunction with the -o option, curl creates the necessary local directory hierarchy as needed.


Video Answer


1 Answers

This output is sent to stderr. So, to get it all you need is redirect stream 2 (stderr) to a file as

curl -o responseFile.html http://www.somewhere.com 2> informationFile.txt

But, as your capture shows, times are not always included.

The better option to know if the request succeded and how long it took is to ask curl to output some internal variables. This is done with the -w switch. So, your command should look as

curl -o responseFile.html http://www.somewhere.com -w "%{response_code};%{time_total}" > dataFile.txt 2> informationFile.txt

That way, the response will go to responseFile.html (or whatever you need), the progress information (stderr or stream 2) will go to informationFile.txt and the required request response code and time information will go to dataFile.txt

like image 121
MC ND Avatar answered Nov 09 '22 09:11

MC ND