shell script/cUrl problem

hello everybody,

i am trying to run curl with the shell script command. however it won’t work. curl keeps telling me that i didn’t specifiy a URL.
thats how i am doing it:

do shell script "curl -d method=user.getInfoByUsername -G -d api_key=<xxxxxx> -d username=" & myUser & " [url=http://upcoming.org/services/rest/]http://upcoming.org/services/rest/"[/url]

the shell command looks ok in the event protocol and when telling terminal “to do script” it’s working.

i guess this must have to do something with:
either the way the shell command is combinated (i tried using “quoted form” with no results)
the space in front of the url?

i tried for hours and couldn’t fix this, nor find any answers helping me.

please help!

-rsh

hi rsh,

the first thing is, does the shell script work in the terminal? you should work on that and get it so everything works outside AppleScript, and then i think you’ll see what’s wrong with the command.

if you get it to work in the terminal, (and still not in AppleScript) then post that by itself so others can compare the two.

It returns a result for me:

set myUser to "anonymous_tester"
do shell script "/usr/bin/curl -d method=user.getInfoByUsername -G -d api_key=anonymous_key -d username=" & myUser & " [url=http://upcoming.org/services/rest/]http://upcoming.org/services/rest/"[/url]

[code]"<?xml version=\"1.0\" encoding=\"UTF-8\"?>
<rsp stat="fail">
<error msg="Your api_key is inactive or not found. Please contact support for assistance." />

"[/code]

@ waltr

yes the command is working fine in the terminal.

@Bruce

seems like the problem is somewhere else, so i’ll post the whole script.

note that it works fine as long as there is no prefs file! (eg. the first run)

the script checks for a prefs file, if there’s none, it asks the user for it’s username and creates one.
then it tries get info about this user via curl.
the goal is to find out a users id#:

-- Set File Path
set PrefsFile to (((path to home folder) as text) & "Library:Preferences:" & "exampleprefs")


-- See if file already exists, if not create new file
try
	open for access file PrefsFile
	set myUser to read file PrefsFile
	close access file PrefsFile
		
on error
	display dialog "Please enter your username..." with icon 1 default answer ""
	set myUser to text returned of result
	my write_to_file(myUser, PrefsFile, false)
end try


on write_to_file(this_data, target_file, append_data)
	try
		set the target_file to the target_file as text
		set the open_target_file to ¬
			open for access file target_file with write permission
		if append_data is false then ¬
			set eof of the open_target_file to 0
		write this_data to the open_target_file starting at eof
		close access the open_target_file
		return true
	on error
		try
			close access file target_file
		end try
		return false
	end try
end write_to_file


do shell script "curl -d method=user.getInfoByUsername -G -d api_key=xxxxxxxx -d username=" & myUser & " [url=http://upcoming.org/services/rest/]http://upcoming.org/services/rest/"[/url]

I have noticed that the use of single quotes with curl inside a script can make a difference. For instance, this will return “Missing Format Variable”

set whichOne to "AAPL"
set myquote to do shell script "curl [url=http://finance.yahoo.com/d/quotes.csv?s=]http://finance.yahoo.com/d/quotes.csv?s="[/url] & whichOne & "&f=l1gh&e=.csv"

Whereas this will come back with the proper quote.

set whichOne to "AAPL"
set myquote to do shell script "curl 'http://finance.yahoo.com/d/quotes.csv?s=" & whichOne & "&f=l1gh&e=.csv'"

Notice the single quotes around the actual URL but not around the curl command? That’s what made the difference in this case.

Can you try playing with that and let me know if it helped?

Matt

Good point matt; I always use quoted form for URLs being passed to curl.

set whichOne to "AAPL"
set myquote to do shell script "curl " & quoted form of ("http://quote.yahoo.com/d/quotes.csv?s=" & whichOne & "&f=l1gh&e=.csv")

ok, i got the script working.

playing with the quotes didn’t resolve the problem.

the strange thing was that the do shell script curl command was working when I set myUser to a string.
it didn’t however when I set myUser to the content of the “preference file” (which was a plain text file) like this:

set myUser to read file PrefsFile

so, i played around and sometime came up with this:

set myUser to read file PrefsFile as Unicode text

now it’s working.
i don’t understand why, though…

Here’s a script that uses the User Defaults system for the preferences.

tell Defaults to set myUser to readKey("username", "")

if result is "" then
	display dialog "Please enter your username." with icon 1 default answer ""
	set myUser to text returned of result
	tell Defaults to writeKey("username", result)
end if

do shell script "curl -d method=user.getInfoByUsername -G -d api_key=xxxxxxxx -d username=" & myUser & " [url=http://upcoming.org/services/rest/]http://upcoming.org/services/rest/"[/url]


script Defaults
	-- Change this to something unique for each script/app
	property _domain : "YourScript"
	
	on readKey(_key, _defaultValue)
		try
			do shell script "/usr/bin/defaults read " & quoted form of _domain & space & quoted form of _key
		on error
			writeKey(_key, _defaultValue)
			return _defaultValue
		end try
	end readKey
	
	on writeKey(_key, _value)
		do shell script "/usr/bin/defaults write " & quoted form of _domain & space & quoted form of _key & space & quoted form of _value
	end writeKey
end script

This might also work (depending on how your script is run).

property username : missing value

if username is missing value then
	display dialog "Please enter your username." with icon 1 default answer ""
	set username to text returned of result
end if

do shell script "curl -d method=user.getInfoByUsername -G -d api_key=xxxxxxxx -d username=" & username & " [url=http://upcoming.org/services/rest/]http://upcoming.org/services/rest/"[/url]

i’m gonna check these out, thanks!