Ruby mechanize post with header

前端 未结 4 460
梦如初夏
梦如初夏 2021-02-04 14:14

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         


        
相关标签:
4条回答
  • 2021-02-04 14:23

    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
    
    0 讨论(0)
  • 2021-02-04 14:39

    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
    
    0 讨论(0)
  • 2021-02-04 14:40

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

    0 讨论(0)
  • 2021-02-04 14:46
    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.

    0 讨论(0)
提交回复
热议问题