{"record":{"id":"f776472672d6ad98","repo":"lostisland/faraday","slug":"can-t-convert-params-class-into-hash","errorCode":null,"errorMessage":"Can't convert #{params.class} into Hash.","messagePattern":"Can't convert #(.+?) into Hash\\.","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"lib/faraday/encoders/flat_params_encoder.rb","lineNumber":29,"sourceCode":"    end\n\n    # Encode converts the given param into a URI querystring. Keys and values\n    # will converted to strings and appropriately escaped for the URI.\n    #\n    # @param params [Hash] query arguments to convert.\n    #\n    # @example\n    #\n    #   encode({a: %w[one two three], b: true, c: \"C\"})\n    #   # => 'a=one&a=two&a=three&b=true&c=C'\n    #\n    # @return [String] the URI querystring (without the leading '?')\n    def self.encode(params)\n      return nil if params.nil?\n\n      unless params.is_a?(Array)\n        unless params.respond_to?(:to_hash)\n          raise TypeError,\n                \"Can't convert #{params.class} into Hash.\"\n        end\n        params = params.to_hash\n        params = params.map do |key, value|\n          key = key.to_s if key.is_a?(Symbol)\n          [key, value]\n        end\n\n        # Only to be used for non-Array inputs. Arrays should preserve order.\n        params.sort! if @sort_params\n      end\n\n      # The params have form [['key1', 'value1'], ['key2', 'value2']].\n      buffer = +''\n      params.each do |key, value|\n        encoded_key = escape(key)\n        if value.nil?\n          buffer << \"#{encoded_key}&\"","sourceCodeStart":11,"sourceCodeEnd":47,"githubUrl":"https://github.com/lostisland/faraday/blob/b25b1b26ccef34b1460b0267115be238ca758087/lib/faraday/encoders/flat_params_encoder.rb#L11-L47","documentation":"Faraday::FlatParamsEncoder.encode turns request params into a query string. It accepts nil, an Array of [key, value] pairs, or any object responding to #to_hash; every other input raises TypeError. The most common violation is passing an already-encoded query String where a Hash is expected, because String responds to neither #to_hash nor is it an Array of pairs.","triggerScenarios":"Calling conn.get('https://api.example.com/search', 'q=ruby&page=2') — the second argument is params, not a query string; passing a JSON string, Integer, or arbitrary object as the params argument of get/post headers-level APIs; handing a custom config object to Faraday that does not implement #to_hash.","commonSituations":"Assuming the second argument to conn.get is a query string (it is a params Hash that gets encoded); porting code from libraries with a different signature; double-encoding bugs where the string then gets escaped again; passing a Struct or OpenStruct-like object that lacks #to_hash.","solutions":["Pass a Hash: conn.get('https://api.example.com/search', q: 'ruby', page: 2).","If you already hold an encoded query string, append it to the URL yourself instead of the params slot: conn.get(\"https://api.example.com/search?#{qs}\") or build a URI and set uri.query.","Pass an Array of pairs when you need repeated keys with ordering: conn.get(url, [%w[q ruby], %w[q rails]]).","For custom param objects, define #to_hash on the class (or call .to_h on the caller side) so the encoder can convert it."],"exampleFix":"# before\nqs = URI.encode_www_form(q: 'ruby', page: 2)\nconn.get('https://api.example.com/search', qs)\n# => TypeError: Can't convert String into Hash.\n\n# after\nconn.get('https://api.example.com/search', q: 'ruby', page: 2)\n# or, with a pre-built query string, put it in the URL:\nconn.get(\"https://api.example.com/search?#{qs}\")","handlingStrategy":"type-guard","validationCode":"params = nil unless params.is_a?(Hash) || params.is_a?(Array) || params.nil?\nconn.get(url, params)","typeGuard":"def encodable_params?(params)\n  params.nil? || params.is_a?(Array) || params.respond_to?(:to_hash)\nend","tryCatchPattern":"begin\n  conn.get(url, params)\nrescue TypeError => e\n  raise unless e.message.include?('into Hash')\n  conn.get(url, params.to_h) # last-resort conversion\nend","preventionTips":["Treat the params argument as Hash-or-Array only; never pass a pre-encoded query string there.","Put pre-built query strings into the URL (\"#{url}?#{qs}\") rather than the params slot.","Add a sorbet/rbs signature or a cheap assert in debug builds: params.nil? || params.respond_to?(:to_hash)."],"tags":["ruby","faraday","params","query-string","typeerror","flat-params-encoder"],"backgroundTag":"invalid-query-params","analyzedSha":"b25b1b26ccef34b1460b0267115be238ca758087","analyzedAt":"2026-08-21T19:43:20.220Z","schemaVersion":2},"datasetVersion":"2026-08-21T23:17:16.201Z"}