0

我有一个类似于THIS的表单,并希望使用 ruby​​ 从 CSV 文件向它提交数据。这是我一直在尝试做的事情:

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)

我一直A valid form ID was not supplied.有一种预感,我使用了错误的 URL,但我不知道用什么来替换它。我会使用 API,但我无权访问令牌,因此我使用了石器时代的方法。任何建议将不胜感激。

4

2 回答 2

1

该表单使用隐藏变量和 cookie 来尝试维护“唯一会话”。幸运的是,Mechanize使处理“偷偷摸摸”的表格变得非常容易。

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
于 2012-04-12T16:17:48.347 回答
-1

查看http://www.formstack.com/forms/?1196833-GxMTxR20GK上的源代码和链接中的示例,看来 formstack 表单发布到 index.php,并且需要传入表单 ID 来识别正在提交哪个表单。查看两个示例中的表单,您会看到与此类似的字段:

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

尝试将以下内容添加到您的参数哈希中:

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

您可能还需要包含其他隐藏字段以进行有效提交。

于 2012-04-12T16:18:41.367 回答