curl and extra commands

Jaguar Applescript.

I’ve checked out the Apple Dev note on cURL and checked our forums here. I don’t think I’m getting the results, but cannot verify what the shell is recieving and what errors it might get. I know for a fact that the uploads are not happening because we always get a confirmation email from the remote site. The remote site is using ipswitch’s DMZ. They got me started on the correct commands.

Here is what works in Terminal, after I removed the escape characters for the quote signs. This is from my results in Event Log.

As noted here in other posts, you have to send the cookie each way to get the authorization to do this. I’ve left off the tacked on request.

So, I’ve got a known good set of commands. I’ve attempted to use a Linefeed and the semicolon. My Event Log only shows the first command results. Where are the results of my second command? What am I doing wrong? thanx, sam


property cr : ASCII character (13)
property lf : ASCII character (10)

tell application "Finder"
	set lst to selection
		set sizeFile to size of (info for (item 1 of lst as alias))

		set fullPathU to (srcFldrUproper & Tfilex)
set Tfilex to "myfile.zip"
		tell me
			do shell script ("curl -k -v -c cookie2.txt \"https://myVENDOR.com/?transaction=signon&username=myUID&password=myPW\";curl -b cookie2.txt -k -v --data-binary @" & quoted form of POSIX path of fullPathU & " -H \"Content-Type: multipart/form-data\" -H \"X-siLock-AgentBrand: cURL\" -H \"X-siLock-AgentVersion: 7.16.2 \" -H \"X-siLock-FolderID:506436921\" -H \"X-siLock-OriginalFilename:" & Tfilex & "\" -H \"X-siLock-FileSize:" & sizeFile & "\" \"https://myVENDOR.com/moveitisapi/moveitisapi.dll?action=upload\"")			
		end tell --me
end tell --finder

thanx, sam

I have no solid advice for you, but maybe one of these ideas will work out.

Most (all?) of curl’s diagnostic output goes to stderr, but do shell script will never show the stderr output unless the last command has an non-zero exit code. So there are two things you might want to do: make the shell abort the script if a non-last command exits with a non-zero exit code, and redirect stderr to stdout so that you can see if curl is trying to tell you something that do shell script would ordinarily quash. You can prepend “set -e;exec 2>&1;” to your command text to accomplish both of these.

The ‘current working directory’ (as output by the pwd command or shell built-in command) is different between a typical Terminal session and do shell script. Terminal starts out in your home directory where you are allowed to create files. do shell script (usually) starts out in the root of your boot drive. If you are not an administrator you may not be able to create files there. The file that caught my eye is the cookie file. You may want to either include a ‘cd’ command to change to your home folder first, or you may want to specify the full path to your cookie file in both curl commands.

The ‘cd or use full-path’ issue also applies to your --data-binary argument, but it looks like you are already taking care of that one by using a full path.

You build the value stored in fullPathU from the values in srcFldrUproper and Tfilex, but immediately after that, you change the value of Tfilex. Is this because you want to “hard code” the value for the X-siLock-OriginalFilename header, or did you intend to hard code the filename of the local file passed to curl’s --data-binary option?

The static headers that you are sending all include spaces before the values, but the dynamic ones do not include a space there. RFC2616 (HTTP/1.1), section 4.2: Message Headers says that the white space is not supposed to matter, but maybe the server to which you are posting requires it.

Is your fullPathU file actually of type multipart/form-data? The curl from Tiger understands “-F fieldname=@/path/to/file” to mean “upload the file into the specified form field name”, but maybe that option does not exist in your curl. If it does, you could probably use it to skip having to manually build the encoded form data.

Last, some AppleScript items.

info for comes from the StandardAdditions OSAX, so it does not need to be in the Finder block. Also, Finder can tell you the size itself, so there is no need for the OSAX. Just tell application “Finder” to set sizeFile to size of item 1 of (get selection) and then put the rest of your code outside any explicit tell block.

You may end up with unexpected values for X-siLock-FileSize if you send large files. I was testing with a 15MB file selected and ended up with “X-siLock-FileSize:1.5973776E+7”. You could use numValue div 1 to try to get an integer, but that only works up to 2^29-1 (536870911; any size below exactly 0.5GiB). Beyond that you are stuck with reals and you would need some way to format them yourself (or at least remove the notation from the standard string representation). Maybe Nigel Garvey’s numToStr would be useful there.

You can use embedded (and shell-escaped) newlines in the do shell script text to make it a bit easier to read:

do shell script ("
curl -k -v \\
	-c cookie2.txt \\
	\"https://myVENDOR.com/?transaction=signon&username=myUID&password=myPW\"
curl -k -v \\
	-b cookie2.txt \\
	--data-binary @" & quoted form of POSIX path of fullPathU & " \\
	-H \"Content-Type: multipart/form-data\" \\
	-H \"X-siLock-AgentBrand: cURL\" \\
	-H \"X-siLock-AgentVersion: 7.16.2 \" \\
	-H \"X-siLock-FolderID:506436921\" \\
	-H \"X-siLock-OriginalFilename:" & Tfilex & "\" \\
	-H \"X-siLock-FileSize:" & sizeFile & "\" \\
	\"https://myVENDOR.com/moveitisapi/moveitisapi.dll?action=upload\"")

When changing stuff around the end of the line, just be sure not to lose the white space before the backslash. Also be sure that the backslash is the last character on the line.

Thank you for your thoughtful response.

This is very helpful and it’s a big help to see the Apple Technote fleshed out with live examples.

My Tfilex variable is out of line where it is. I just need the path explicitly and the filename itself because it appears either curl or the ftp site is expecting a filename within its own quotes. Thus if I escaped it, it would be uploaded to the vendor as " ‘myfilename’ "

Truth be told, my problems are a mixture, mostly my inexperience with the command line. Once I understand how the command is structured then I could use the combined std output to piece together what I was doing wrong. I was aware of “set -e;exec 2>&1;” but it was not clear where it would go. Your response answered that query. Thanks, again.