AnimalCode
AnimalCode

Reputation: 101

Ruby mechanize post with header

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

Upvotes: 9

Views: 13765

Answers (4)

Nakilon
Nakilon

Reputation: 35093

Seems like earlier 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

Upvotes: 4

avout
avout

Reputation: 204

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'
  }

Upvotes: 11

Kris
Kris

Reputation: 19948

ajax_headers = { 'X-Requested-With' => 'XMLHttpRequest', 'Content-Type' => 'application/json; charset=utf-8', 'Accept' => 'application/json, text/javascript, */*'}
params = {'emailAddress' => '[email protected]'}.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.

Upvotes: 8

Robert Duncan
Robert Duncan

Reputation: 510

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

Upvotes: 2

Related Questions