vero
vero

Reputation: 1015

Command curl malformed in shell script

Why when I use curl in ssh session it works well, but when I use it in a shell script it return an error.

In ssh session:

[root@XXXX scripts]# export host_name="http://XXXX:number_port"
[root@XXXX scripts]# echo "curl -u GET ${host_name}/application/listteam"
curl -u GET http://XXXX:number_port/application/listteam

But when I use it in a shell script:

list_team=$(curl GET "${host_name}/application/listteam")
echo "$list_team"

It retrun:

 % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0curl: (6) Could not resolve host: GET; Name or service not known
curl: (3) <url> malformed

I see that could not resolve host, but why when I try in ssh session it works ?

Some idea please ?

Upvotes: 0

Views: 1885

Answers (2)

Kenster
Kenster

Reputation: 25399

curl -u GET ${host_name}/application/listteam"             # interactive
list_team=$(curl GET "${host_name}/application/listteam")  # non-interactive

In the first example, you have "-u GET". The "-u" parameter requires an argument, and curl is interpreting the word "GET" as that argument. It then goes on to interpret the next command-line argument as the URL to fetch.

In the second example, you just have "GET" without any "-u". curl is interpreting the word "GET" to be one of the URLs which it should fetch. You're getting an error because it can't interpret the word "GET" as a valid URL.

"-u" is used to specify a user name and password for the request. If you really need to specify the word "GET" as a user name, then your second example should have "-u GET" just like the first example.

On the other hand, if the word "GET" is supposed to be the type of HTTP request to make, then you should use "-X GET". Or just leave it out, because curl will do a GET request by default.

Upvotes: 3

Mishi.Srivastava
Mishi.Srivastava

Reputation: 390

Maybe because you are using name_host as variable in shell script instead of host_name.

Also make sure you can acess that variable through shell script.

Upvotes: 0

Related Questions