Curl won't complete in a script but does from prompt. Idea?

On RHEL5 from within both a shell (sh->bash) and a csh script curl fails with a "curl: (6) Couldn't resolve host 'application'" error.

This is the result whether run as a command at the shell and/or c-shell prompt and curl also fails with the same error when the scripts are "source'd" at the prompts (".scriptname" at sh prompt; "source scriptname" at csh prompt).

the curl command completes with the "Created" message, however, if it is copied and pasted to the shell or csh prompt. Frustrating! Looking for other reasonable things to try.

This is the curl command:

curl -i -H "Content-Type: application/json" -X POST -d @CurlData ${CurlAddr}

The "-d @CurlData" file is:

{
"A":"bbb",
"L":"mmm",
"X":"yyy",
}

The "${CurlAddr}" variable is:

http://<address>:<port>/A/B/C/post

We've googled around and tried a number of the experiments: removing the space after"Content-Type: "; adding an "Accept: ..." -H Header; having the data expressed on the command-line ( -d '{...}' ); having the address expressed on the command-line.

All combos fail with the "Can't resolve" error from scripts but complete from the prompt.

Has anyone been there before and found a solution?
Thanks.
Geo. Salisbury
Long Valley, NJ

can you update your CURL to latest ?
yum - Upgrade cURL to latest on CentOS - Server Fault

Hi,

Can you try running the script with the -x flag (i.e. bash -x ./script.sh , for instance), and see what the actual curl command it's running looks like ?

batcherr:
curl was yum updated: curl -V = curl 7.15.5

drysdalk
The script has been run with the "-x" option - result:

/usr/bin/curl -i -H '"Content-Type:' 'application/json"' -X POST -d @CurlData http://address:port/A/B/C/post

The "curl: (6) Couldn't resolve host 'application'" error remains when coming from [a] script whether executed or sourced.

The command continues complete with a "Created" success message when copied and pasted to the command-line.

grrrr
Geo.

Hi,

OK, well...that at least clarifies precisely what is going wrong, if not why. The fact it's saying:

Couldn't resolve host 'application'

can only mean that it's interpreting the 'application/json"' bit of your input as the URL to connect to (i.e., it thinks the server is quite literally 'application', and the URL you're trying to retrieve is 'application/json').

That doesn't get us directly closer to explaining why this works just fine at the command line, though.

Next questions, then:

  • Is the curl in your own PATH at the shell prompt the same /usr/bin/curl as the script is trying to execute (i.e. check the output of which curl ?
  • Is the shell the script is running as the same as your logon shell (i.e. does the shebang #!/ line tell the script to run as the same shell as the one you're using at your own command prompt ?

Hopefully the answers to these questions will lead us a bit closer to what's going on here.

---------- Post updated at 07:56 PM ---------- Previous update was at 07:45 PM ----------

Hi,

Right, I can actually replicate this. TL;DR - it's the quotes.

So, with my script reading thusly:

#!/bin/bash
/usr/bin/curl -i -H '"Content-Type:' 'application/json"' -X POST -d @CurlData http://127.0.0.1:80/bogus/URL/here

I got (amongst other things) the following in the output:

$ ./script.sh
Warning: Couldn't read data from file "CurlData", this makes an empty POST.
curl: (6) Could not resolve host: application

If I change the script to this:

#!/bin/bash
/usr/bin/curl -i -H "Content-Type: application/json" -X POST -d @CurlData http://127.0.0.1:80/bogus/URL/here

I get this:

$ ./script.sh
Warning: Couldn't read data from file "CurlData", this makes an empty POST.
HTTP/1.1 404 Not Found
Date: Mon, 13 Mar 2017 19:55:25 GMT
Server: Apache/2.4.18 (Ubuntu)
Content-Length: 287
Content-Type: text/html; charset=iso-8859-1

(which obviously I'd expect to, since I have neither data to post nor anything to post it to).

Try changing the formatting of your quotes and see how you get on, maybe ?

Yes, curl is on the path.
We did "which curl" at the outset so the we could use the "/usr/bin/curl" consistently.

The shell has been both the same as login (csh) as well as sh (which is linked to bash). The shebang has been consistent with the script syntax: #!/bin/sh in the shell script and #!/bin/csh -f in the C-shell script. We have the two only as a means to try to isolate the problem. The shell script is what is likely to be used.

The edit addon concerning the quotes lead us to a resolution - thanks!!

We had been assembling a string that included the quoted content-type header so that we could echo it out to the display.
That was how we got the command to copy/paste to the prompt.

We tweaked our demo script to put the header to a variable and this curl command now works from the script:

${CurlPgm} -i -H "${CurlHdr}" -X POST -d @CurlData ${CurlAddr}/post

The CurlPgm variable contains the /usr/bin/curl and the CurlHdr variable contains the Content-Type: application/json the CurlData and CurlAddr remain as before.

Again thanks for the quotes clue.
Very much appreciated.
As you could well surmise we spent a considerable amount of "why won't this work" time for what [typically] turned-out to be an easy fix. Phew!

I'd mark this solved but I don't think I know how to do that.
Again thanks.
Geo.