Ruby mechanize post with header

前端 未结 4 461
梦如初夏
梦如初夏 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: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.

提交回复
热议问题