Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot response HTTP code 204 when using context.json() #1518

Closed
PlayerNguyen opened this issue Sep 28, 2023 · 11 comments · May be fixed by #3763
Closed

Cannot response HTTP code 204 when using context.json() #1518

PlayerNguyen opened this issue Sep 28, 2023 · 11 comments · May be fixed by #3763
Labels

Comments

@PlayerNguyen
Copy link

What version of Hono are you using?

3.7.2

What runtime/platform is your app running on?

Node

What steps can reproduce the bug?

  • Setup a Node/Hono server
  • Implement below code
const app = new Hono();
app.delete('...', async (context: Context) => {
  const categoryId = context.req.param("category-id");

 const 

  // If not found the category
  if (response === 0) {
    context.status(204);
    return context.json(
        { data: [], message: t("routes.video_categories.category_not_found") }
    );
  }
  return context.json({ id: categoryId });
});

// Hosting the app
  • Request DELETE / and an exception was occurred
TypeError: Response constructor: Invalid response status code 204

What is the expected behavior?

I expected this implemented code should response 204 (No content) data

What do you see instead?

TypeError: Response constructor: Invalid response status code 204

Additional information

No response

@vanodevium
Copy link

When status code is 204, Response can't contain any content. Body is empty always.

@datashaman
Copy link

datashaman commented Oct 1, 2023

HTTP Status 204 signifies that there is no content in the response body.
https://http.dev/204

Given your code, what you want to use is status 404 instead, which is usually used when you cannot find a record, where you can include a body.
https://http.dev/404

@yusukebe
Copy link
Member

Hi @PlayerNguyen

Is this problem resolved? If you wan to return 204 with no body, you can return c.body(null, 204).

@yusukebe
Copy link
Member

Closing. If you have any problems, please reopen

@askorupskyy
Copy link

@yusukebe just encountered this. While it's an easy fix, this was missed in one of my PRs and caused a few errors in prod. Do you think it could be a good idea to omit the 204 from acceptable response codes in c.json() and whatnot?

@EdamAme-x
Copy link
Contributor

Yes @askorupskyy.
You can delete body

@askorupskyy
Copy link

askorupskyy commented Dec 19, 2024

What I had in mind is doing something like

json: JSONRespond = <
    T extends JSONValue | SimplifyDeepArray<unknown> | InvalidJSONValue,
    U extends Exclude<StatusCode, '204'> = Exclude<StatusCode, '204'>
  >

This way TypeScript would let us know if we're trying to do something that would cause errors in runtime

@EdamAme-x
Copy link
Contributor

Interesting idea

@yusukebe
Copy link
Member

Hi @askorupskyy @EdamAme-x

Throwing a TypeScript error if the Response has a status code that can't have a null body is a good idea. If so, it should support other status codes, not only 204:

CleanShot 2024-12-21 at 16 38 48@2x

@askorupskyy
Copy link

@yusukebe i will make a pr for this soon

@EdamAme-x
Copy link
Contributor

EdamAme-x commented Dec 21, 2024

That is correct.
But there is one problem, and it complicates the types somewhat.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants