question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Return JSON response with custom status code

See original GitHub issue

Hello,

I’m building a JSON api using spirit. I’ve started by adding catch-all route to return 404 response.

route.get("*", {
  status: 404,
  headers: {
    'Content-Type': 'application/json'
  }
    body: JSON.stringify({
    message: 'Not Found',
  })
})

While it works, it’s a little bit verbose.

Is there a shorter way to do that, maybe something like:

route.get("*", response.json(404, { message: "Not Found" }))

Thanks, Ran.

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

2reactions
hnrycommented, Nov 3, 2016
route.not_found({ message: "Not found" }) 
// or for GET only requests
route.not_found("get", { message: "Not found" })

will produce a response with status code 404, and json content type header set, and a json body.

Also as per your suggestion @ranyefet I updated the response helper function to be:

route.not_found(response({ message: "Not found" }).type("json"))
// or 
route.get("*", response({ message: "Not found" }).type("json").status_(404))

spirit v0.4.0 & spirit-router v0.4.0

Closing, if there’s an issue or another suggestion feel free to let me know ✌️

2reactions
hnrycommented, Nov 1, 2016

Hey @ranyefet ,

You can do route.get("*", ...) to catch GET only, or there is also route.not_found(...) which will 404 on every request regardless of method. (Ex: route.not_found("Page is not found") or notFound is camelCase alias to it.

You mentioned the explicit way, there’s also @dodekeract suggestion.

You also mentioned “response.json”, there is a helper function similar to that:

const {response} = require("spirit").node
const route = require("spirit-router")

const app = route.define([
  route.not_found(response(JSON.stringify({ message: "Not found" })).type("json")),
  // or
  route.get("*", response(JSON.stringify({ message: "Not found" })).type("json").status_(404))
])

Also this should work (but it doesn’t, so will need to fix):

route.not_found({ message: "Not found" }) 

Which would do what you want. Will upload fix by tonight (spirit-router@0.4.0).

Read more comments on GitHub >

github_iconTop Results From Across the Web

Return JSON with error status code MVC - Stack Overflow
A simple way to send a error to Json is control Http Status Code of response object and set a custom error message....
Read more >
Return JSON Result with Custom Status Code in ASP.NET Core
NET Core responses and learn how to return a JSON result with custom status code through the help of formatters or directly from...
Read more >
HTTP status and error codes for JSON | Cloud Storage
HTTP status and error codes for JSON · 302—Found · 303—See Other · 304—Not Modified · 307—Temporary Redirect · 308—Resume Incomplete · 400—Bad...
Read more >
how to return JSON and a response status like 201 #1520
I have a POST route that I want to return JSON and a response status of ... you could also use status::Custom to...
Read more >
Return a Response Directly - FastAPI
But you can return a JSONResponse directly from your path operations. It might be useful, for example, to return custom headers or cookies....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found