{"record":{"id":"e4eaa58583e3430c","repo":"puppetlabs/puppet","slug":"post-requires-a-string-body-argument-e4eaa5","errorCode":null,"errorMessage":"'post' requires a string 'body' argument","messagePattern":"'post' requires a string 'body' argument","errorType":"validation","errorClass":"ArgumentError","httpStatus":null,"severity":"error","filePath":"lib/puppet/http/external_client.rb","lineNumber":42,"sourceCode":"\n    client = @http_client_class.new(url.host, url.port, options)\n    response = Puppet::HTTP::ResponseNetHTTP.new(url, client.get(url.request_uri, headers, options))\n\n    if block_given?\n      yield response\n    else\n      response\n    end\n  rescue Puppet::HTTP::HTTPError\n    raise\n  rescue => e\n    raise Puppet::HTTP::HTTPError.new(e.message, e)\n  end\n\n  # (see Puppet::HTTP::Client#post)\n  # @api private\n  def post(url, body, headers: {}, params: {}, options: {}, &block)\n    raise ArgumentError, \"'post' requires a string 'body' argument\" unless body.is_a?(String)\n\n    url = encode_query(url, params)\n\n    options[:use_ssl] = url.scheme == 'https'\n\n    client = @http_client_class.new(url.host, url.port, options)\n    response = Puppet::HTTP::ResponseNetHTTP.new(url, client.post(url.request_uri, body, headers, options))\n\n    if block_given?\n      yield response\n    else\n      response\n    end\n  rescue Puppet::HTTP::HTTPError, ArgumentError\n    raise\n  rescue => e\n    raise Puppet::HTTP::HTTPError.new(e.message, e)\n  end","sourceCodeStart":24,"sourceCodeEnd":60,"githubUrl":"https://github.com/puppetlabs/puppet/blob/e227c27540975c25aa22d533a52424a9d2fc886a/lib/puppet/http/external_client.rb#L24-L60","documentation":"Puppet::HTTP::ExternalClient (the client used by puppetserver-side code that wraps a supplied HTTP client class) mirrors the internal client's precondition for POST: the body argument must be a String, else ArgumentError is raised before the wrapped client is ever invoked.","triggerScenarios":"Calling external_client.post(url, nil, headers: ...); passing a Hash or a Net::HTTP-readable object; JRuby/puppetserver code or gems built on the external client (e.g. puppet-http style wrappers) forwarding unserialized bodies.","commonSituations":"Migrating puppetserver middleware from raw Net::HTTP where any object with to_s worked; forgetting JSON.generate in bolt/task handlers; double-wrapping clients so a body string becomes a Response object.","solutions":["Serialize the payload to a String before calling post.","Default nil bodies to '' so absent payloads do not raise.","Keep header/body preparation in one place so both the internal and external client paths receive identical strings."],"exampleFix":"# before\nexternal_client.post(url, { 'state' => 'done' }, headers: { 'Content-Type' => 'application/json' })\n\n# after\nexternal_client.post(url, JSON.generate(state: 'done'), headers: { 'Content-Type' => 'application/json' })","handlingStrategy":"type-guard","validationCode":"body = JSON.generate(data) unless body.is_a?(String)\nexternal_client.post(url, body, headers: { 'Content-Type' => 'application/json' })","typeGuard":"string_body = ->(b) { b.is_a?(String) }\nraise ArgumentError, 'body must be a String' unless string_body.call(body)","tryCatchPattern":null,"preventionTips":["Share one body/header preparation helper between internal and external client paths.","Never pass raw Ruby objects; render to a String first."],"tags":["puppet","http","post","external-client","request-validation"],"backgroundTag":"wrong-argument-type","analyzedSha":"e227c27540975c25aa22d533a52424a9d2fc886a","analyzedAt":"2026-08-21T20:49:46.650Z","schemaVersion":2},"datasetVersion":"2026-08-21T23:17:16.201Z"}