2
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?

Failed to parse URL from ~

2
Posted at

はじめに

fetchを使って、api以下を取得するさいにエラーが起きました。

問題

page
export default async function DDDTestPage() {
    const response = await fetch("/api/users/active", {
        method: "GET"
    });
    const data = await response.json();
    console.log("APIレスポンス:", data);
    return <div>DDD test completed</div>;
}

解決方法

Next.jsを使っているのでサーバサイドでレンダリングする方法を使いました。
next/headersから現状のプロトコルとホストを取得して利用します。

page
import { headers } from 'next/headers';


export default async function DDDTestPage() {
    const headersData = await headers();
    const protocol = headersData.get('x-forwarded-proto') || 'http';
    const host = headersData.get('host');
    const response = await fetch(`${protocol}://${host}/api/users/active`, {
        method: "GET"
    });
    console.log("APIレスポンス:", response);
    return <div>DDD test completed</div>;
}

おわりに

次にいきます

参考文献

2
0
2

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
2
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?