# Adding cookies to my http client

**URL:** <https://www.ruby-forum.com/t/adding-cookies-to-my-http-client/191431>\
**Category:** Ruby\
**Created:** [July 12, 2010, 4:48pm UTC](https://www.ruby-forum.com/t/adding-cookies-to-my-http-client/191431 "2010-07-12T16:48:02Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![razta](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/ec9cab/32.png) [@razta](https://www.ruby-forum.com/u/razta)\
**Post date:** [July 12, 2010, 4:48pm UTC](https://www.ruby-forum.com/t/adding-cookies-to-my-http-client/191431/1 "2010-07-12T16:48:02Z")

</div>

Hi,  
The following is a modified version of the http client found in the  
o’reilly ruby cookbook.

Does any one know how I can add custom cookies to this?

class HTTP  
# Makes an HTTP reuqest and returns the HTTPRespnse object.  
# Args: :proxy\_host, :proxy\_port, :action (:get, :post, etc.),  
# :data (for :post action), :max\_redirects.

```
def HTTP.fetch(uri, args={}.freeze, &before_fetching)
  # Process the arguments with default values
  uri = URI.parse(URI.encode(uri)) unless uri.is_a? URI
  proxy_host = args[:proxy_host]
  proxy_port = args[:proxy_port] || 80
  action = args[:action] || :get
  data = args[:data]
  max_redirects = args[:max_redirects] || 10
  cookies = args[:cookies] || ''

  # Set http headers
  headers = {'Cookie' => cookies}

  # Use proxy class to create the request object
  proxy_class = Proxy(proxy_host, proxy_port)
  request = proxy_class.new(uri.host, uri.port)

  # Organise the query
  if action == 'get'
    query = uri.path.to_s + "?" + uri.query.to_s
  else
    query = uri.path.to_s + '/'
  end

     response = request.send(action, query, data)

  return response

end

```

Thank you in advance!
