puppetlabs/puppet · error · ArgumentError

'post' requires a string 'body' argument

Error message

'post' requires a string 'body' argument

What it means

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.

Source

Thrown at lib/puppet/http/external_client.rb:42

    client = @http_client_class.new(url.host, url.port, options)
    response = Puppet::HTTP::ResponseNetHTTP.new(url, client.get(url.request_uri, headers, options))

    if block_given?
      yield response
    else
      response
    end
  rescue Puppet::HTTP::HTTPError
    raise
  rescue => e
    raise Puppet::HTTP::HTTPError.new(e.message, e)
  end

  # (see Puppet::HTTP::Client#post)
  # @api private
  def post(url, body, headers: {}, params: {}, options: {}, &block)
    raise ArgumentError, "'post' requires a string 'body' argument" unless body.is_a?(String)

    url = encode_query(url, params)

    options[:use_ssl] = url.scheme == 'https'

    client = @http_client_class.new(url.host, url.port, options)
    response = Puppet::HTTP::ResponseNetHTTP.new(url, client.post(url.request_uri, body, headers, options))

    if block_given?
      yield response
    else
      response
    end
  rescue Puppet::HTTP::HTTPError, ArgumentError
    raise
  rescue => e
    raise Puppet::HTTP::HTTPError.new(e.message, e)
  end

View on GitHub (pinned to e227c27540)

Solutions

  1. Serialize the payload to a String before calling post.
  2. Default nil bodies to '' so absent payloads do not raise.
  3. Keep header/body preparation in one place so both the internal and external client paths receive identical strings.

Example fix

# before
external_client.post(url, { 'state' => 'done' }, headers: { 'Content-Type' => 'application/json' })

# after
external_client.post(url, JSON.generate(state: 'done'), headers: { 'Content-Type' => 'application/json' })
Defensive patterns

Strategy: type-guard

Validate before calling

body = JSON.generate(data) unless body.is_a?(String)
external_client.post(url, body, headers: { 'Content-Type' => 'application/json' })

Type guard

string_body = ->(b) { b.is_a?(String) }
raise ArgumentError, 'body must be a String' unless string_body.call(body)

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Related errors


AI-assisted analysis of puppetlabs/puppet@e227c27540 (2026-08-21). Data as JSON: /api/errors/e4eaa58583e3430c. Report an issue: GitHub.