Ruby mechanize post with header

笑着哭i 提交于 2019-12-03 09:40:50

问题


I have page with js that post data via XMLHttpRequest and server side script check for this header, how to send this header?

agent = WWW::Mechanize.new { |a|
  a.user_agent_alias = 'Mac Safari'
  a.log = Logger.new('./site.log')
}

agent.post('http://site.com/board.php',
  {
    'act' => '_get_page',
    "gid" => 1,
    'order' => 0,
    'page' => 2
  }
) do |page|
  p page
end

回答1:


I found this post with a web search (two months later, I know) and just wanted to share another solution.

You can add custom headers without monkey patching Mechanize using a pre-connect hook:

  agent = WWW::Mechanize.new
  agent.pre_connect_hooks << lambda { |p|
    p[:request]['X-Requested-With'] = 'XMLHttpRequest'
  }




回答2:


ajax_headers = { 'X-Requested-With' => 'XMLHttpRequest', 'Content-Type' => 'application/json; charset=utf-8', 'Accept' => 'application/json, text/javascript, */*'}
params = {'emailAddress' => 'me@my.com'}.to_json
response = agent.post( 'http://example.com/login', params, ajax_headers)

The above code works for me (Mechanize 1.0) as a way to make the server think the request is coming via AJAX, but as stated in other answers it depends what the server is looking for, it will be different for different frameworks/js library combos.

The best thing to do is use Firefox HTTPLiveHeaders plugin or HTTPScoop and look at the request headers sent by the browser and just try and replicate that.




回答3:


Seems like earlier versions of Mechanize that lambda had one argument, but now it has two:

agent = Mechanize.new do |agent|
  agent.pre_connect_hooks << lambda do |agent, request|
    request["Accept-Language"] = "ru"
  end
end



回答4:


Take a look at the documentation.

You need to either monkey-patch or derive your own class from WWW::Mechanize to override the post method so that custom headers are passed through to the private method post_form.

For example,

class WWW::Mechanize
  def post(url, query= {}, headers = {})
    node = {}
    # Create a fake form
    class << node
      def search(*args); []; end
    end
    node['method'] = 'POST'
    node['enctype'] = 'application/x-www-form-urlencoded'

    form = Form.new(node)
    query.each { |k,v|
      if v.is_a?(IO)
        form.enctype = 'multipart/form-data'
        ul = Form::FileUpload.new(k.to_s,::File.basename(v.path))
        ul.file_data = v.read
        form.file_uploads << ul
      else
        form.fields << Form::Field.new(k.to_s,v)
      end
    }
    post_form(url, form, headers)
  end
end

agent = WWW::Mechanize.new

agent.post(URL,POSTDATA,{'custom-header' => 'custom'}) do |page|
    p page
end


来源:https://stackoverflow.com/questions/1327495/ruby-mechanize-post-with-header

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!