Domanda

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'         => 'bip@bob.com',
      '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.

È stato utile?

Soluzione

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@address.com" 
                end
end

page = form.submit form.buttons.first

puts
puts "=== Response Header"
puts
puts page.header
puts
puts "=== Response Body"
puts
puts page.body

Altri suggerimenti

Looking at the source on http://www.formstack.com/forms/?1196833-GxMTxR20GK and the example in your link, it appears that formstack forms post to index.php, and require a form id to be passed in to identify which form is being submitted.. Looking at the forms in both examples, you'll see a field similar to this:

<input type="hidden" name="form" value="1196833" />

Try adding the following to your params hash:

'form' => '1196883' # or other appropriate form value

You may also need to include the other hidden fields for a valid submit.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top