我正在尝试模拟与此网页上的表单相关的http请求: http://www.ei.applipub-fft.fr/eipublic/competitionRecherche.do 然后我需要解析响应html。
我试图使用gem' webmock'和'休息 - 客户'所以在研究了原始的http请求后,我尝试了类似的东西:
def post
stub_request(:post, "http://www.ei.applipub-fft.fr/eipublic/competitionRecherche.do").
with(:body => {"data"=>{"dispatch"=>"filtrer", "hoi_atp"=>"T", "dtdate"=>"30/12/2015", "mois"=>"12", "multi_lig_cod"=>"0", "lig_cno_1"=>"01", "bidf"=>"0", "imit_categ_age"=>"0", "hoiCtmcDames"=>"T"}},
:headers => {'Accept'=>'*/*; q=0.5, application/xml', 'Accept-Encoding'=>'gzip, deflate', 'Content-Length'=>'174', 'Content-Type'=>'application/x-www-form-urlencoded', 'User-Agent'=>'Ruby'}).
to_return(:status => 200, :body => "", :headers => {})
resp = RestClient.post('http://www.ei.applipub-fft.fr/eipublic/competitionRecherche.do', "data[dispatch]=filtrer&data[hoi_atp]=T&data[dtdate]=30/12/2015&data[mois]=12&data[multi_lig_cod]=0&data[lig_cno_1]=01&data[bidf]=0&data[imit_categ_age]=0&data[hoiCtmcDames]=T",
:content_type => 'application/x-www-form-urlencoded')
puts resp a
end
但我无法接受正确的HTML回复。 有人可以解释这一切是如何运作的吗?
答案 0 :(得分:0)
我通常会使用HTTParty gem。它允许您在Ruby中编写发布请求。 例如:
HTTParty.post('http://example.com/post_here',
body: { subject: 'This is the screen name',
description: 'This is the description for the problem'
}.to_json,
headers: { 'Content-Type' => 'application/json' } )
答案 1 :(得分:0)
发表:
require "net/http"
require "uri"
uri = URI.parse("http://example.com/search")
# Shortcut
response = Net::HTTP.post_form(uri, {"q" => "My query", "per_page" => "50"})
# Full control
http = Net::HTTP.new(uri.host, uri.port)
request = Net::HTTP::Post.new(uri.request_uri)
request.set_form_data({"q" => "My query", "per_page" => "50"})
response = http.request(request)
处理响应对象:
response.code # => 301
response.body # => The body (HTML, XML, blob, whatever)
# Headers are lowercased
response["cache-control"] # => public, max-age=2592000