Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

warning: overriding "Cookie" header with :cookies option #822

Open
MR-X-junior opened this issue Jun 9, 2023 · 0 comments
Open

warning: overriding "Cookie" header with :cookies option #822

MR-X-junior opened this issue Jun 9, 2023 · 0 comments

Comments

@MR-X-junior
Copy link

I want to do a post request to a page https://mbasic.facebook.com

But when I do that, a warning pops up

warning: overriding "Cookie" header with :cookies option

But when I use :cookies as headers, my code doesn't work. On the other hand when I use cookies as headers my code works fine.

Below is my code:

require 'nokogiri'
require 'rest-client'

class Session
  def initialize(headers = {})
    @headers = headers
    @cookies = {}
    @options = {
      verify_ssl: true, # Tambahkan ini jika Anda menghadapi masalah SSL
      max_redirects: 5, # Batasan pengalihan maksimum
      timeout: 10, # Batas waktu permintaan dalam detik
      open_timeout: 5, # Batas waktu koneksi dalam detik
      user_agent: 'python-requests/2.28.2', # Ganti dengan User-Agent yang diinginkan
      follow_redirect: true
    }
  end

  def get(url, params = {}, headers = {}, options = {})
    request(:get, url, params, headers, nil, options)
  end

  def post(url, data = {}, headers = {}, options = {})
    request(:post, url, {}, headers, data, options)
  end

  def put(url, data = {}, headers = {}, options = {})
    request(:put, url, {}, headers, data, options)
  end

  def delete(url, headers = {}, options = {})
    request(:delete, url, {}, headers, nil, options)
  end

  def head(url, headers = {}, options = {})
    request(:head, url, {}, headers, nil, options)
  end

  private

  def request(method, url, params = {}, headers = {}, data = nil, options = {})
    headers = @headers.merge(headers)
    headers['Cookie'] = @cookies.map { |k, v| "#{k}=#{v}" }.join('; ') unless @cookies.empty?

    response = RestClient::Request.execute(method: method,url: url,headers: headers,payload: data,params: params,**@options.merge(options)) do |response|
      if [301,302, 307].include? (response.code)
        response.follow_redirection
      else
        response.return!
      end
    end

    # Perbarui cookie setelah menerima respons
    update_cookies(response.cookies)

    response
  end

  def update_cookies(cookie_header)
    return if cookie_header.nil? || cookie_header.empty?

    cookie_header.each do |key, value|
      @cookies[key] = value
    end
  end
end

email = "example@example.com"
pass = "password123"

sesi = Session.new({'User-Agent': 'Mozilla/5.0 (Linux; Android 8.1.0; vivo 1802) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/107.0.0.0 Mobile Safari/537.36', 'Accept': '*/*', 'Connection': 'keep-alive'})

req = sesi.get("https://mbasic.facebook.com")
res = Nokogiri::HTML(req.body)
data = {}

res.search('input').each{|i| data.update({i['name']=>i['value']})}

data.delete_if{|k,v| (k == "sign_up" or k.nil?)}
data.update({"email"=>email,"pass"=>pass})
url = "https://mbasic.facebook.com#{res.at('form')['action']}"

submit = sesi.post(url, data = data)

How to remove the warning message?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant