0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

curlしたら404 Not Foundが返った【Hono】

0
Posted at

はじめに

HonoでPOSTのエンドポイントを作成し、動作確認のためcurlを叩いたところ404 Not Foundが返ってきました。

問題

実行したコマンド

curl -X POST 'http://localhost:3000/api/todos' -H 'Content-Type: application/json' -d '{"todo": "study"}'

# 404 Not Found

元のコード

app.post("/api/todos", async (c) => {
  try {
    const body = await c.req.json();
    console.log(body);
  } catch {
    return c.json({ error: "failed to create todo" });
  }
});

解決方法

tryの中でreturnした。

app.post("/api/todos", async (c) => {
  try {
    const body = await c.req.json();
    console.log(body);
    return c.json({ body });
  } catch {
    return c.json({ error: "failed to create todo" });
  }
});

おわりに

returnしないと404が返ってくるのを知りませんでした。何も返ってこないだけかと思っていました。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?