Next 13 App Router - simple API request failing
Answered
Abyssinian posted this in #help-forum
AbyssinianOP
I have a basic webapp where a user uploads a PDF on the client, the pdf is sent to my API endpoint (on the server) and then a JSON object is returned. File structure:
src/app
api
parse-pdf.ts
page.tsx
I am using
src/app
api
parse-pdf.ts
page.tsx
# page.txt
'use client'
...
const sendFile = async (file: File) => {
// Convert the file to ArrayBuffer
const arrayBuffer = await fileToArrayBuffer(file);
const response = await fetch('/api/parse-pdf', {
method: 'POST',
headers: { 'Content-Type': 'application/pdf'},
body: arrayBuffer
});
if (response.ok) {
const json = await response.json();
} else {
console.error('Failed to process PDF.');
}
};# parse-pdf.ts
import { NextResponse } from 'next/server';
export async function Post(request: Request) {
const pdfBuffer = await request.arrayBuffer();
// Do something with the PDF buffer here...
const result = {message: "PDF processed successfully."};
return NextResponse.json(
{ result },
{
status: 200,
},
);
}I am using
npm run dev to run this locally. When I upload a PDF, I get the errors shown in the photo. It looks like its not even locating the endpoint? What am I doing wrong? Thank you!Answered by New Zealand Heading Dog
Just to check, are you using the app router or pages router?
If you are using the app router, this is how your folder structure should look:
If you are using the app router, this is how your folder structure should look:
- app
--- api
----- parse-pdf
------- route.ts
--- page.tsx13 Replies
@ncls. You are calling the wrong URL (404)
AbyssinianOP
are you refering to
If so, what should it be? Thank you!
/api/parse-pdfIf so, what should it be? Thank you!
New Zealand Heading Dog
Just to check, are you using the app router or pages router?
If you are using the app router, this is how your folder structure should look:
If you are using the app router, this is how your folder structure should look:
- app
--- api
----- parse-pdf
------- route.ts
--- page.tsxAnswer
@Abyssinian I am using the App Router.
New Zealand Heading Dog
Then the above folder structure applies. The reason you are getting the 404 error is because the parse-pdf is a ts file, not a folder.
@Abyssinian are you refering to
ts
/api/parse-pdf
If so, what should it be? Thank you!
That should be the URL, yes and I think the function name has to be all uppercase (so
POST instead of Post)@ncls. That should be the URL, yes and I think the function name has to be all uppercase (so `POST` instead of `Post`)
New Zealand Heading Dog
Yeah, this too.
@New Zealand Heading Dog Then the above folder structure applies. The reason you are getting the 404 error is because the parse-pdf is a ts file, not a folder.
AbyssinianOP
Thank you for your prompt response. Given the file structure you suggested, does this mean that the parse-pdf folder can only contain one .ts file dealing with that endpoint (i.e. route.ts)?
@Abyssinian Thank you for your prompt response. Given the file structure you suggested, does this mean that the parse-pdf folder can only contain one .ts file dealing with that endpoint (i.e. route.ts)?
New Zealand Heading Dog
As far as I know, yes. Each API route has to be a folder with a route.ts file inside.
route.ts is the only file Next.js cares about in there. You can have other files in there and import them but they won't be loaded by Next.js if you don't import them into route.ts@ncls. `route.ts` is the only file Next.js cares about in there. You can have other files in there and import them but they won't be loaded by Next.js if you don't import them into `route.ts`
New Zealand Heading Dog
Nice to know. Thank you for that information.
AbyssinianOP
Great, this worked for me!
@Abyssinian Great, this worked for me!
New Zealand Heading Dog
Make sure to mark the solution. Glad to help!