`encode_www_form':未定义的方法`map'

时间:2014-03-31 05:42:27

标签: ruby

当我尝试执行以下代码时

require "net/http"
require "uri"

@appArgs = Array.new
@vmip_and_port = "yyyyy"

def post(url, data)
  uri = URI.parse(url)
  http = Net::HTTP.new(uri.host, uri.port)
  request = Net::HTTP::Post.new(uri.request_uri)
  request.set_form_data(data)
  response = http.request(request)
  return response
end

def click_inapp(id)
  @appArgs.push(id)
  return
end

def finish_inapp()
  data =  @appArgs.join(",")
  res = post("http://" +@vmip_and_port+"/", data)
  return res.body
end
click_inapp("5X7_photo_paper.png")
click_inapp("4X6_photo_paper.png")
finish_inapp()

我得到了

uri/common.rb:931:in `encode_www_form': undefined method `map' for "5X7_photo_paper.png,4X6_photo_paper.png":String (NoMethodError)
    from /home/ushusadmin/.rvm/rubies/ruby-1.9.3-p448/lib/ruby/1.9.1/net/http.rb:1761:in `set_form_data'
    from app_utilities.rb:11:in `post'
    from app_utilities.rb:23:in `finish_inapp'
    from app_utilities.rb:28:in `<main>'

我的代码有什么问题。请给我核心

1 个答案:

答案 0 :(得分:3)

set_form_data期待Hash字段名称和值,而不是string。所以试着:

require "net/http"
require "uri"

@appArgs = Hash.new
@vmip_and_port = "172.16.2.40:9090"

def post(url, data)
  uri = URI.parse(url)
  http = Net::HTTP.new(uri.host, uri.port)
  request = Net::HTTP::Post.new(uri.request_uri)
  request.set_form_data(data)
  response = http.request(request)
  return response
end

def click_inapp(field_name, id)
  @appArgs[field_name] = id
end

def finish_inapp()
  res = post("http://" +@vmip_and_port+"/", @appArgs)
  return res.body
end
click_inapp("png1", "5X7_photo_paper.png")
click_inapp("png2", "4X6_photo_paper.png")
finish_inapp()

我不确定该计划的其余部分,因为我不想在172.16.2.40:9090测试和攻击您的服务器: - )

在控制台上尝试ri Net::HTTP,以获得示例。