Re: do shell script, curl and cookies
Re: do shell script, curl and cookies
- Subject: Re: do shell script, curl and cookies
- From: Christopher Nebel <email@hidden>
- Date: Thu, 18 Apr 2013 23:48:46 -0700
On Apr 11, 2013, at 16:14:57 +0200, HÃ¥vard Graudo wrote:
> I have problems using cookies with curl via do shell script.
>
> I want to login and get a cookie, then send commands back to the server, including the cookie.
>
> This works in Terminal:
> curl --cookie-jar zecookie.txt 'http://www.example.com/login?username=bob&password=pass'
> --> login OK, sessionID: 1i7nm0ch6pphezdqkulhdub30 blah blah
> curl --cookie zecookie.txt 'http://www.example.com/search?q=something'
> --> returns my search. all nice and dandy
>
> The information stored in the cookie is a plain Netscape HTTP Cookie File.
>
> However if I do the SAME THING using 'do shell script' it fails:
> do shell script "curl --cookie-jar zecookie.txt 'http://www.example.com/login?username=bob&password=pass'"
> --> login OK, sessionID: s04988pspn6otuaaz6xeuooy blah blah
> do shell script "curl --cookie zecookie.txt 'http://www.example.com/search?q=ssomething'"
> -- NOT LOGGED IN
>
> What works in Terminal does not with 'do shell script'.
>
> Can someone help me in the right direction here?
At this point, you've got a solution thanks to other folks, but the explanation has been a bit confused. I'm surprised no one has mentioned TN2065, "do shell script in AppleScript" <http://developer.apple.com/library/mac/#technotes/tn2065> yet; it has a bunch of pointers about using "do shell script", including some tips about the working directory.
The basic caveat about "do shell script" versus Terminal is that, unless you're running your script from Terminal, you don't get most of the environment you have in your shell in Terminal, which includes the "working directory". (See the very end of TN2065 for more about this.) I'm guessing a bit here, because curl(1) doesn't print out any error message about this, but the issue you had is probably that curl(1) failed to write out the cookie file because you don't have write permission to the location you gave it, which, since it's just a filename, is in the working directory, which if you were running it from AppleScript Editor, is "/". Giving it an explicit path to someplace you *do* have write permission for makes it work.
There's nothing mysterious or random about the working directory. As TN2065 describes, it's simply inherited from whatever process executed the "do shell script" (which inherits it from its parent process, and so on). If you'd run your original script using osascript(1) from Terminal, it would have worked fine, because it would have inherited the working directory of the shell, probably "~".
The advice about not relying on the working directory in scripts has to do with the fact that it's been known to change between OS versions -- for instance, it used to be the directory containing the application -- and may be different if the application was launched in an unusual way (such as from Terminal or Xcode). The simple fact is that the working directory isn't relevant to your typical application -- Cocoa works independently of it. That said, if you intend for your script to be run from Terminal (either using osascript or as a self-contained "#!" executable), relying on the working directory can be quite useful, in the same ways it's useful to any other shell script.
--Chris N.
AppleScript Engineering
PS: It's perfectly fine to use "~" to mean the home directory in a "do shell script" command. The command is handed off to /bin/sh to execute, which knows how to expand the "~" properly.
_______________________________________________
Do not post admin requests to the list. They will be ignored.
AppleScript-Users mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
Archives: http://lists.apple.com/archives/applescript-users
This email sent to email@hidden