Visit The New, Modern Unix Linux Community


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


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Curl won't complete in a script but does from prompt. Idea?
# 1  
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:
Code:
curl -i -H "Content-Type: application/json" -X POST -d @CurlData ${CurlAddr}

The "-d @CurlData" file is:
Code:
{
"A":"bbb",
"L":"mmm",
"X":"yyy",
}

The "${CurlAddr}" variable is:
Code:
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
# 2  
Quote:
Originally Posted by GSalisbury
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:
Code:
curl -i -H "Content-Type: application/json" -X POST -d @CurlData ${CurlAddr}

The "-d @CurlData" file is:
Code:
{
"A":"bbb",
"L":"mmm",
"X":"yyy",
}

The "${CurlAddr}" variable is:
Code:
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
# 3  
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 ?
# 4  
batcherr:
curl was yum updated: curl -V = curl 7.15.5

drysdalk
The script has been run with the "-x" option - result:
Code:
/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.
# 5  
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:

Code:
#!/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:

Code:
$ ./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:

Code:
#!/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:

Code:
$ ./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 ?
# 6  
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:
Code:
${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.

Previous Thread | Next Thread
Thread Tools Search this Thread
Search this Thread:
Advanced Search

Test Your Knowledge in Computers #80
Difficulty: Easy
Many operating systems today, including Android, Chrome OS, Steam OS, and myriad embedded systems for devices, are based on Linux.
True or False?

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Bash script won't run because hardware won't produce display

Can anyone offer any advice on how to modify the script below to work on a new system we have, that has no graphics capability? We admin the system through a serial RAS device. I've tried running the below script through the RAS and through an ssh -X session. It failed with something like "GTK... (3 Replies)
Discussion started by: yelirt5
3 Replies

2. Shell Programming and Scripting

Access a complete flow with CURL + bash shell

Hello Experts , I have an use case which needed your help . I have been using google for 2 days buy couldn`t succed , i believe i can get the help here. Here is my use case to run on bash shell 1. Access an URL -- in script , it will be mentioned as inputURL 2. Once i accessed the URL... (5 Replies)
Discussion started by: radha254
5 Replies

3. Shell Programming and Scripting

Access a complete flow with CURL + bash shell

Hello Experts , I have an use case which needed your help . I have been using google for 2 days buy couldn`t succed , i believe i can get the help here. Here is my use case to run on bash shell 1. Access an URL -- in script , it will be mentioned as inputURL 2. Once i accessed the URL... (1 Reply)
Discussion started by: radha254
1 Replies

4. HP-UX

File system full, won't complete boot

Hello, I have a very old hp rp2450 running 11i2 that I had to power off. When turning it back on it hangs with the following message. Would booting into SUM get me any further? I've never used SUM, so I'm asking before I go down that rabbit hole. Thanks for the help. # ## extra content... (1 Reply)
Discussion started by: calabaria
1 Replies

5. Shell Programming and Scripting

need a new idea for a script

Hi all, I now have project in UNIX Solaris and I want to have some new ideas to execute it, so I hope you help me finding new ideas in scripting or some infrastructure .bye (1 Reply)
Discussion started by: hard_revenge
1 Replies

6. Shell Programming and Scripting

Bash script idea using cUrl -- possible?

I have an alias already in my .bash_profile to download files using cUrl's -o (output to file, user provides the file name) option. I find I'm using it quite a bit, so I wanted to write a script to run "curl -o", taking the necessary inputs - file name and URL from which to download - and then... (3 Replies)
Discussion started by: SilversleevesX
3 Replies

7. UNIX for Dummies Questions & Answers

BASH complete-filename & menu-complete together

Hi, Does anyone know how to make BASH provide a list of possible completions on the first tab, and then start cycling through the possibilites on the next tab? Right now this is what I have in my .bashrc: bind "set show-all-if-ambiguous on" bind \\C-o:menu-complete This allows... (0 Replies)
Discussion started by: Mithu
0 Replies

8. AIX

won't mount /usr...won't boot fully

Hello: NOOB here. I attempted to use smit mkcd. Failed on first attempt, not enough space. 2nd attempt tried to place iso on /usr, not enough space there. Cleanup ran for about 5 minutes after aborting. Now AIX won't boot. LCD display on 7029-6E3 says: 0517 MOUNT /USR. Attempted to boot from CD... (11 Replies)
Discussion started by: bbird
11 Replies

9. Shell Programming and Scripting

Script Idea's / Help

Hi there, I'm pretty new to scripting and wondering if anyone had any idea's, scripts or snippets on how I can do the following. I basically want a shell script that will look at all the files in a directory and find all the names and addresses in them then output them to the screen nicely... (12 Replies)
Discussion started by: mrpugster
12 Replies

10. Shell Programming and Scripting

Limitations of awk? Good idea? Bad idea?

Keeping in mind that I'm relatively comfortable with programming in general but very new to unix and korn/bourne shell scripts.. I'm using awk on a CSV file, and then performing calculations and operations on specific fields within specific records. The CSV file I'm working with has about 600... (2 Replies)
Discussion started by: yongho
2 Replies

Featured Tech Videos