[development] Curl-based content generation

Martin Stadler martin at siarp.de
Sat Feb 7 15:34:54 UTC 2009

I was pretty close. I needed to add the security token and the form ID.

What I was really searching for was this documentation about cURL and  
Drupal: http://drupal.org/node/80548 . I'm basically using this now  
but originally I wanted to get along with a shell script only.  
Actually I'm trying to export images from iPhoto to Drupal using  
AppleScript as I didn't want to dive into plugin development and you  
can call shell scripts from AppleScript.

What I learned: The security token (form_token) depends on the session  
ID (plus form ID and private key) and thus you need to create a  
session with the client you want to use to create the content (or  
create a cookie with such session ID). Just send a request for the add- 
page and extract the token from the HTML form. This token can be used  
from now on to add content using a tool like cURL.

Checking the resulting HTML pages I also got confused because the  
redirect did not work with cURL so I didn't get the success message I  
expected though the creation was successful.

Here's my working cURL code:

# login
curl http://localhost/drupal/?q=user \
	-s \
	-c cookie.txt \
	-b cookie.txt \
	-F 'name=user' \
	-F 'pass=pass' \
	-F 'form_id=user_login' \
	-F 'op=Log in' \
	--output response0.html

# get form
curl http://localhost/drupal/?q=node/add/page \
	-s \
	-c cookie.txt \
	-b cookie.txt \
	--output response1.html
# -> extract token from response1.html (/edit-page-node-form-form- 
token" *value="([^"]*)"/)

# add page
# -> use extracted token
curl http://localhost/drupal/?q=node/add/page \
	-s \
	-c cookie.txt \
	-b cookie.txt  \
	-F 'title=xyz' \
	-F 'body=abc' \
	-F 'form_id=page_node_form' \
	-F 'form_token=63fe773e820d2a4565720ab3bd0fc991' \
	-F 'status=1' \
	-F 'revision=1' \
	-F 'op=Save' \
	--output response2.html

More information about the development mailing list