rest-client / rest-client

Simple HTTP and REST client for Ruby, inspired by microframework syntax for specifying actions.

Home Page:https://rubydoc.info/github/rest-client/rest-client/master

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

warning: overriding "Cookie" header with :cookies option

MR-X-junior opened this issue · comments

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?