#Kappa my friend, how do I get nitro to always return JSON even when accessed via browser?

9 messages · Page 1 of 1 (latest)

glad drift
#

Also for errors

tacit perchBOT
#

Hi @glad drift. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave:

#

The knowledge sources don’t describe a way to globally force Nitro to always return JSON (including for HTML page routes) or to globally switch the default error handler to JSON responses.

What is mentioned:

  • Nitro’s default error handler does not include some fields (statusMessage, data) in API responses in production, which is why Nuxt passes those through separately to error.vue for page rendering. [pass statusMessage; pass error data]
    This shows how Nuxt customizes error handling for page rendering, but it doesn’t document a JSON-only mode.

Because there’s no explicit documentation in these sources for:

  • A global “JSON mode” for all Nitro routes, or
  • A config flag to make all errors return JSON instead of HTML,

I can’t reliably give you a concrete config snippet that will do exactly what you want.

If you need JSON-only behavior, the documented/known approaches (outside these sources) are typically:

  • Implementing your own API routes under server/api/** that explicitly return JSON.
  • Implementing custom Nitro middleware / handlers that set the response type and body.

But those patterns are not shown in the provided documents, so I can’t quote an exact implementation here.

Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:

#

@glad drift Kapa.ai is still learning and improving, please let me know how I did by reacting below.

glad drift
#

ugh, so there is no way to have JSON? I tried with:

setResponseHeader(event, "content-type", "application/json")

but erros still show up as html

#

@tacit perch ?

tacit perchBOT
#

Let me check the knowledge base for you again. One moment! :books:

#

The provided knowledge sources don’t describe a way to make all Nitro responses (including normal page routes) always return JSON, nor a global switch to force all errors to be JSON.

What they do show is:

  • Nuxt customizes how error information is passed into error.vue for page rendering, because Nitro’s default API error responses in production don’t include statusMessage and data. [pass statusMessage; pass error data]
    This is about enriching data for the error page, not about forcing JSON output.

There is no mention in these sources of:

  • A Nitro or Nuxt config option like “always respond with JSON”, or
  • A documented pattern for globally overriding Nitro’s default error handler to JSON for every route.

Because of that, I can’t give you a source-backed snippet that guarantees “Nitro always returns JSON, even in the browser, including for errors.” The knowledge sources simply don’t cover this use case.

Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:

#

@glad drift Kapa.ai is still learning and improving, please let me know how I did by reacting below.