Search code examples
rubyurinet-httpformstack

How to submit formstack form using ruby?


I have a form similiar to THIS and want to be submit data to it from a CSV file using ruby. Here is what I have been trying to do:

require 'uri'
require 'net/http'

params = {
      'field15157482-first'   => 'bip',
      'field15157482-last'    => 'bop',
      'field15157485'         => '[email protected]',
      'field15157487'         => 'option1'
      'fsSubmitButton1196962' => 'Submit'
}

x = Net::HTTP.post_form(URI.parse('http://www.formstack.com/forms/?1196833-GxMTxR20GK'), params)

I keep getting A valid form ID was not supplied. I have a hunch I am using the wrong URL but I don't know what to replace it with. I would use the the API but I don't have access to the token hence my stone age approach. Any suggestions would be much appreciated.


Solution

  • The form uses hidden variables and cookies to attempt to maintain a "unique session". Fortunately, Mechanize makes handling 'sneaky' forms quite easy.

    require "mechanize"
    form_uri = "http://www.formstack.com/forms/?1196962-617Z6Foyif"
    
    @agent = Mechanize.new
    page = @agent.get form_uri
    
    form = page.forms[0]
    
    form.fields_with(:class => /fsField/).each do |field|
      field.value = case field.name
                      when /first/ then "First Name"
                      when /last/ then "Last Name"
                      else "[email protected]" 
                    end
    end
    
    page = form.submit form.buttons.first
    
    puts
    puts "=== Response Header"
    puts
    puts page.header
    puts
    puts "=== Response Body"
    puts
    puts page.body