I use this command to fill out a form in Drupal 6 (create node):

curl -b cookies.txt -d "title=thetitle&menu%5Blink_title%5D=&menu%5Bparent%5D=primary-links%3A0&menu%5Bweight%5D=0&teaser_include=1&body=content+here&format=1&changed=&form_build_id=form-01fbf44be3dab1ea177d17544bce415c&form_token=f1af1b01946065a34e49fdbde8fcc64b&form_id=story_node_form&log=&comment=2&pathauto_perform_alias=1&field_pidio%5B0%5D%5Bembed%5D=&name=admin&date=&status=1&promote=1&op=Save" http://localhost/subdo/node/add/story

It works without any problem; no messages in the terminal and the node is created in Drupal as expected.

After a while, I use that code again without any modification. But instead of posting a new node, the terminal shows the HTML code of the form. It looks like cURL isn't hitting the "Save" button.

  • Not it solved. It was the form token changes – lokomika Jul 24 '11 at 15:06
  • Drupal adds a hidden form field that contains a form token; if the token is not the one Drupal expects, then the form is not accepted. It's probable that in that case Drupal shows again the same form to get the data, if it is able to understand which form has been submitted. – kiamlaluno Jul 24 '11 at 19:17

Drupal forms are often two-step affairs requiring a preview/approval stage. The form you are seeing is identical and pre-filled out with the values you submitted from the first one, but there will also be a preview of the action on the page and a hidden form field that indicates that the next form submission is being submitted from the preview so the data should actually be saved.

The difference is typically the name of the submit button. On forms that require preview, the submit button will pass the operation as op=Preview and from the form on the preview page it will send op=Save. The good news is that it doesn't actually track this preview stage server-side, it just uses clues from the form to know what step you are at. When submitting with an automated fashion like you are trying to do from like curl, you can skip straight to the real save by including sending op=Save instead of using the value from the initial form.

Additionally, Drupal forms are usually tracked using a unique identifier or token. In order to submit these forms automatically, you will need to download the page with curl, parse it for the form field that contains the unique token (called form_token in DP7), then include that key/value pair in your POST.

At least in Drupal 7 you should have no problem combining these techniques. The form token does not change from the preview to the final, so once you fetch the token you can skip straight to the final save POST.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.