Arun Arun - 1 month ago 25
JSON Question

cURL download files issue

When i give the URL (

http://192.168.150.41:8080/filereport/31779/json/
) in browser, It automatically downloads the file as
31779_report.json
.

Now using i'm trying to download the file using
curl
but i get the following error.

$ curl -O http://192.168.150.41:8080/filereport/31779/json/
curl: Remote file name has no length!
curl: try 'curl --help' or 'curl --manual' for more information


When using the '
-L
' switch , I get the JSON content displayed but the file is not saved.

$curl -L http://192.168.150.41:8080/filereport/31779/json/

{

.....
.....

}


How to download the exact file "
31779_report.json
" using cURL / wget ?

I don't want the contents to be redirected (
>
) manually to a file (
31779_report.json
).

Any suggestions please ?

Answer

The -O flag of curl tries to use the remote name of the file, but because your URL schema does not end with a filename, it can not do this. The -o flag (lower-case o) can be used to specify a file name manually without redirecting STDOUT like so:

curl <address> -o filename.json

You can manually construct the filename format you want using awk. For example:

URL=http://192.168.150.41:8080/filereport/31779/json/
file_number=$(echo $URL | awk -F/ '{print $(NF-2)}')
file_name="${file_number}_report.json"
curl -L "$URL" -o "$file_name"

Hope this is more helpful.