Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

cURL download files issue

Tags:

json

curl

wget

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 ?

like image 267
Arun Avatar asked Oct 21 '16 11:10

Arun


2 Answers

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.

like image 128
Jack Bracken Avatar answered Sep 29 '22 21:09

Jack Bracken


wget --content-disposition did the trick for me (https://askubuntu.com/a/77713/18665)

$ wget --content-disposition https://www.archlinux.org/packages/core/x86_64/lib32-glibc/download/
...
Saving to: 'lib32-glibc-2.33-4-x86_64.pkg.tar.zst'

Compare to curl:

$ curl -LO https://www.archlinux.org/packages/core/x86_64/lib32-glibc/download/
curl: Remote file name has no length!
curl: (23) Failed writing received data to disk/application

And wget without --content-disposition:

$ wget https://www.archlinux.org/packages/core/x86_64/lib32-glibc/download/
...
Saving to: 'index.html'
like image 24
bmaupin Avatar answered Sep 29 '22 20:09

bmaupin