Using CURL to upload a file via HTTP form

Hello everyone,

I’m running into a bit of a problem with a workflow I’m trying to complete at work.

I’m reading some data from a document and then passing that information into a form in Safari. That part works fine.

The form creates another form with a file upload button; I can’t seem to figure out how to successfully manipulate that via Safari, so if anyone has any ideas or suggestions I’d love to hear them.

I did some research and I think that doing this through Safari is not possible, so I looked into the possibility of uploading the file via HTTP by using Curl. I figured out that I’d need to log on first and get cookies set up, so that part is done.

I’m trying to pass the following command to upload the file “test1.jpg”

curl -b cookies.txt -F “filefield=@/test1.jpg;type=image/jpg” -F “aiimage2=cj216200993826PM.jpg”

The form’s file identifier is “filefield”, and “aiimage” is an identifier for the filename that will be used after the image has been uploaded (their server renames files to avoid duplications and so forth)

However, when I execute this command with the verbose mode on, this is what gets returned:

  • About to connect() to port 80 (#0)
  • Trying 216...*… connected
  • Connected to (216...*) port 80 (#0)

POST /admin/photoupdate_upload.asp HTTP/1.1
User-Agent: curl/7.16.3 (powerpc-apple-darwin9.0) libcurl/7.16.3 OpenSSL/0.9.7l zlib/1.2.3
Accept: /
Content-Length: 67174
Expect: 100-continue
Content-Type: multipart/form-data; boundary=----------------------------113569baa18b

< HTTP/1.1 100 Continue
< Server: Microsoft-IIS/5.0
< Date: Tue, 17 Feb 2009 03:16:16 GMT
< HTTP/1.1 500 Internal Server Error
< Server: Microsoft-IIS/5.0
< Date: Tue, 17 Feb 2009 03:16:17 GMT
< Content-Length: 351
< Content-Type: text/html
< Cache-control: private

Microsoft OLE DB Provider for ODBC Drivers error '80040e14'

[Microsoft][ODBC SQL Server Driver][SQL Server]Incorrect syntax near '='.

I’m not sure what the problem is since everything else seems to be working swimmingly. This is the only thing that’s holding up my applescript from working! Any ideas/suggestions would be very very welcome.

Hi all,

I managed to figure this out on my own by going back into the MacScripter archives!

tell application "Safari"
	do JavaScript "document.forms[0].filefield.value = \"" & "/test1.jpg\"" in document 1
	do JavaScript "document.forms[0].submit()" in document 1
end tell

Worked like a charm! I was trying this the other night but it didn’t seem to be working, however I was missing out on the fact that the file selection was not being updated in the Safari window; supplying the information and passing the submit appears to have worked though.

Cheers MacScripter community!

Gah, I must have goofed when I was testing this out – I believe I used Safari to check the file then the doJavascript submit command, thinking that I had solved the problem, but as I have since discovered, the doJavascript command will not pass information to the input type=file html form…

I think the last ditch thing I can do is try to use GUI scripting to click that button and select the file… it’ll be something to experiment with today.

If anyone else has a brainstorm I’d love to hear it.

Incidentally, when I was experimenting with this yesterday, I discovered that old versions of Firefox could be used with SystemEvents to simulate typing in a file name manually. This doesn’t work with Safari and it doesn’t work with Firefox 3.