hoppscotch/hoppscotch · error · Error

charset() expects a string value (typically Content-Type hea

Error message

charset() expects a string value (typically Content-Type header)

What it means

Thrown by the Postman-compatible `charset()` assertion on the chai proxy (`pm.expect(value).to.have.charset(expected)`). It fires when `expectVal` is not a string — `charset()` expects the value under test to be a string (typically the Content-Type header). Any non-string `expectVal` (number, object, undefined, null) triggers it.

Source

Thrown at packages/hoppscotch-js-sandbox/src/bootstrap-code/post-request.js:1822

                if (error) return `Item [${i}]: ${error}`
              }
            }
          }

          return null // Validation passed
        }

        const error = validateSchema(expectVal, schema)
        if (error) {
          throw new Error(`JSON Schema validation failed: ${error}`)
        }
        return withModifiers(modifiers)
      }

      proxy.charset = (expectedCharset) => {
        // expectVal should be a string (typically Content-Type header)
        if (typeof expectVal !== "string") {
          throw new Error(
            "charset() expects a string value (typically Content-Type header)"
          )
        }

        const lowerExpected = expectedCharset.toLowerCase()
        const lowerActual = expectVal.toLowerCase()

        if (!lowerActual.includes(lowerExpected)) {
          throw new Error(
            `Expected charset "${expectedCharset}" not found in "${expectVal}"`
          )
        }
        return withModifiers(modifiers)
      }

      proxy.cookie = (cookieName, cookieValue) => {
        // This works when expectVal is pm.response or similar
        // For the Chai extension, we need to check if cookies exist

View on GitHub (pinned to 1acb8a3a75)

Solutions

  1. Pass the Content-Type header string as `expectVal`, not the response object.
  2. Confirm the header exists before asserting (guard against undefined).
  3. Use the correct chai chain so `expectVal` resolves to a string.
  4. Switch to a header-specific assertion if you actually need the full header.

Example fix

// before
pm.expect(pm.response).to.have.charset("utf-8") // response is an object

// after
pm.expect(pm.response.headers["content-type"]).to.have.charset("utf-8")
Defensive patterns

Strategy: type-guard

Validate before calling

const ct = pm.response.headers["content-type"]
if (typeof ct !== "string") throw new Error("content-type header is not a string")
pm.expect(ct).to.have.charset("utf-8")

Type guard

function isString(v: unknown): v is string { return typeof v === "string" }

Try / catch

// assertion error surfaced by the sandbox test runner

Prevention

When it happens

Trigger: Calling `.charset(...)` on a chai assertion whose `expectVal` is not a string — e.g. `pm.expect(pm.response).to.have.charset('utf-8')` (object) or `pm.expect(123).to.have.charset('utf-8')` instead of `pm.expect(pm.response.headers['content-type']).to.have.charset('utf-8')`.

Common situations: Passing the response object instead of the Content-Type header value, asserting before headers are populated, or a typo in the chai chain that leaves `expectVal` as a non-string.

Related errors


AI-assisted analysis of hoppscotch/hoppscotch@1acb8a3a75 (2026-08-12). Data as JSON: /api/errors/be6e767133f6c0e6. Report an issue: GitHub.