{"record":{"id":"4fc37c12e9837003","repo":"jnunemaker/httparty","slug":"cookies-must-be-an-object-which-responds-to-to-ha","errorCode":null,"errorMessage":"Cookies must be an object which responds to #to_hash","messagePattern":"Cookies must be an object which responds to #to_hash","errorType":"exception","errorClass":"ArgumentError","httpStatus":null,"severity":"error","filePath":"lib/httparty.rb","lineNumber":252,"sourceCode":"\n    # Allows setting HTTP headers to be used for each request.\n    #\n    #   class Foo\n    #     include HTTParty\n    #     headers 'Accept' => 'text/html'\n    #   end\n    def headers(h = nil)\n      if h\n        raise ArgumentError, 'Headers must be an object which responds to #to_hash' unless h.respond_to?(:to_hash)\n        default_options[:headers] ||= {}\n        default_options[:headers].merge!(h.to_hash)\n      else\n        default_options[:headers] || {}\n      end\n    end\n\n    def cookies(h = {})\n      raise ArgumentError, 'Cookies must be an object which responds to #to_hash' unless h.respond_to?(:to_hash)\n      default_cookies.add_cookies(h)\n    end\n\n    # Proceed to the location header when an HTTP response dictates a redirect.\n    # Redirects are always followed by default.\n    #\n    # @example\n    #   class Foo\n    #     include HTTParty\n    #     base_uri 'http://google.com'\n    #     follow_redirects true\n    #   end\n    def follow_redirects(value = true)\n      default_options[:follow_redirects] = value\n    end\n\n    # Allows setting the format with which to parse.\n    # Must be one of the allowed formats ie: json, xml","sourceCodeStart":234,"sourceCodeEnd":270,"githubUrl":"https://github.com/jnunemaker/httparty/blob/8f4a09e343b94de9f934f388028ca97620c9b378/lib/httparty.rb#L234-L270","documentation":"The class-level DSL method `cookies` raises ArgumentError unless its argument responds to #to_hash. The value is handed to default_cookies.add_cookies, which merges it into a CookieHash applied to every request, so only Hash-like input is accepted. Note that a CookieHash or a plain Hash both work because both respond to to_hash.","triggerScenarios":"`cookies 'sessionid=abc123'` (raw Cookie-header string) inside a class that includes HTTParty, or `cookies nil`, `cookies [:a, :b]` at class level. Distinct from the per-request `get url, cookies: { ... }` option, which flows through the same guard when merged.","commonSituations":"Developers pasting a browser Cookie header string from devtools, loading cookie jars from JSON/YAML without parsing, and assuming Set-Cookie strings can be replayed directly.","solutions":["Pass name/value pairs as a Hash: `cookies sessionid: 'abc123'`.","Convert a raw cookie string: `cookies Hash[raw.split('; ').map { |c| c.split('=', 2) }]`.","Keep cookies from a prior response: assign `cookies response.headers['Set-Cookie']` only after parsing it, or use the cookie_hash from the response object."],"exampleFix":"# before\ncookies 'sessionid=abc123; theme=dark'   # String -> ArgumentError\n\n# after\ncookies 'sessionid' => 'abc123', 'theme' => 'dark'","handlingStrategy":"validation","validationCode":"raise ArgumentError, 'cookies must be a Hash' unless jar.respond_to?(:to_hash)\ncookies jar.to_hash","typeGuard":"hash_like = ->(v) { v.respond_to?(:to_hash) }","tryCatchPattern":"begin\n  cookies jar\nrescue ArgumentError\n  raise ConfigError, 'pass cookies as { \"name\" => \"value\" }, not a Cookie header string'\nend","preventionTips":["Parse Set-Cookie strings before replaying them.","Store cookie jars as plain Hashes in config.","Remember HTTParty merges and re-serializes cookies itself; feed it name/value pairs."],"tags":["ruby","httparty","configuration","validation","argument-error","cookies"],"backgroundTag":"invalid-argument-type","analyzedSha":"8f4a09e343b94de9f934f388028ca97620c9b378","analyzedAt":"2026-08-21T19:30:42.003Z","schemaVersion":2},"datasetVersion":"2026-08-21T23:17:16.201Z"}