I am trying to retrieve data from a local API endpoint route within my SvelteKit application. I can confirm the endpoint works by navigating directly to it at http://localhost:5173/api/getSummary?summaryId=20 where I can see the JSON data as expected.
In a separate route - /(authenticated)/importSummary/+page.svelte
- I import a component in the same directory that utilizes this API endpoint:
<script lang="ts"> export async function showInfo() { const createSummary_response = await fetch("/api/getSummary/?summaryId=20", { method: "GET", }); const createSummary_response_data = await createSummary_response.json(); console.log("createSummary_response_data:", createSummary_response_data); }</script>
This works as expected and I receive the data. My problem is when I try to access this same endpoint from a +page.server.ts
, in this case at /(authenticated)/manageSummary/+page.server.ts
:
import type { PageServerLoad } from './$types'export const load: PageServerLoad = async ({ fetch, params }) => { const getSummary_response = await fetch(`/api/getSummary/?summaryId=20`, { method: "GET", }); const getSummary_response_data = getSummary_response; console.log("getSummary_response_data:", getSummary_response_data);}
to which I receive the following response:
getSummary_response_data: Response { status: 308, statusText: '', headers: Headers { 'x-sveltekit-normalize': '1', location: '/api/getSummary?summaryId=20' }, body: null, bodyUsed: false, ok: false, redirected: false, type: 'default', url: ''}
Logging in my /api/getSummary/+server.ts
file suggests that this endpoint is not being reached at all when fetched from the +page.server.ts
.