I am a noob when it comes to anything related to backend, also new in Next.js and TypeScript.
I am trying to make a POST request to an API, which should receive formData and create a new listing based on that formData. I am trying to do this with Next.js server actions and route handlers.
Here is my code:
This is from the form component:
const handleSubmit = async (e: React.FormEvent<HTMLFormElement>) => {
e.preventDefault();
const form = e.currentTarget as HTMLFormElement;
const formData = new FormData(form);
const address = formData.get("address");
const mail = formData.get("mail");
const region_id = selectedRegion?.id; //these come from states
const agent_id = selectedAgent?.id; //these come from states
const city_id = selectedCity?.id; //these come from states
const area = formData.get("area");
const price = formData.get("price");
const type = formData.get("type");
const cover = formData.get("cover"); // This is the file input
try {
console.log(address, mail, region_id, agent_id, city_id, area, price, type, cover);
const newFormData = new FormData();
newFormData.append("address", address as string);
newFormData.append("mail", mail as string);
newFormData.append("region_id", region_id as any);
newFormData.append("agent_id", agent_id as any);
newFormData.append("city_id", city_id as any);
newFormData.append("area", area as string);
newFormData.append("price", price as string);
newFormData.append("type", type as string);
newFormData.append("cover", cover as File);
await addListingAction(newFormData)
} catch (error) {
alert(`Error: ${error}`);
}
};
This is my server action:
export async function addListingAction(formData: FormData) {
try {
await addListing(formData);
} catch (error) {
console.log(error);
} finally {
revalidatePath("/")
}
}
my api.ts file:
export async function addListing(formData: FormData) {
return await fetch(`${process.env.NEXT_PUBLIC_API_URL}/api/add-listing`, {
method: 'POST',
body: formData,
})
.then(response => response.json())
.then(data => console.log(data))
.catch(error => console.error('Error:', error));
}
and my route.ts file:
import { NextResponse } from 'next/server';
export async function POST(req: Request) {
try {
const formData = await req.formData();
const address = formData.get("address");
const mail = formData.get("mail");
const region_id = formData.get("region_id");
const agent_id = formData.get("agent_id");
const city_id = formData.get("city_id");
const area = formData.get("area");
const price = formData.get("price");
const type = formData.get("type");
const cover = formData.get("cover") as File | null;
const form = new FormData();
if (address) form.append("address", address);
if (mail) form.append("mail", mail);
if (region_id) form.append("region_id", region_id);
if (agent_id) form.append("agent_id", agent_id);
if (city_id) form.append("city_id", city_id);
if (area) form.append("area", area);
if (price) form.append("price", price);
if (type) form.append("type", type);
if (cover instanceof File) form.append("cover", cover);
const REDBERRY_API_TOKEN = process.env.REDBERRY_API_TOKEN;
if (!REDBERRY_API_TOKEN) {
throw new Error("Missing API token");
}
const response = await fetch('https://api.real-estate-manager.redberryinternship.ge/api/real-estates', {
method: 'POST',
headers: {
'Authorization': `Bearer ${REDBERRY_API_TOKEN}`,
},
body: form,
});
if (!response.ok) {
let errorResponse;
try {
errorResponse = await response.json();
} catch {
errorResponse = await response.text();
}
console.error('HTTP error! Status:', response.status, 'Response:', errorResponse);
return NextResponse.json(
{ message: 'API request failed', error: errorResponse },
{ status: response.status }
);
}
const listing = await response.json();
return NextResponse.json({ listing }, { status: 201 });
} catch (error) {
const message = error instanceof Error ? error.message : 'An unexpected error occurred';
console.error('Error creating listing:', message);
return NextResponse.json(
{ message: 'Failed to create listing', error: message },
{ status: 500 }
);
}
}
The API documentation says that the curl request looks like this:
curl -X 'POST'
'https://api.real-estate-manager.redberryinternship.ge/api/real-estates'
-H 'accept: application/json'
-H 'Authorization: Bearer API_TOKEN'
-H 'Content-Type: multipart/form-data'
-F 'region_id=1'
-F 'price=100000'
-F 'zip_code=0101'
-F 'area=100.5'
-F 'city_id=1'
-F 'address=example address'
-F 'agent_id=371'
-F 'bedrooms=3'
-F 'is_rental=0'
-F '[email protected];type=image/png'
-F 'description=house near metro station'
and the response looks like this:
"price": "100000",
"zip_code": "0101",
"area": "100.5",
"city_id": "1",
"address": "example description",
"agent_id": "371",
"bedrooms": "3",
"is_rental": "0",
"description": "house near metro station",
"image": "https://api.real-estate-manager.redberryinternship.ge/storage/images/vZ6KRjLiBIoVqwl4GMniE598mAzno8wqzCkZyg3f.png",
"created_at": "2024-09-15T16:43:21.000000Z",
"id": 450
The error I am getting is:
Error creating listing: Unexpected token ‘<‘, “<!DOCTYPE “… is not valid JSON
POST /api/add-listing 500 in 931ms
{
message: ‘Failed to create listing’,
error: ‘Unexpected token ‘<‘, “<!DOCTYPE “… is not valid JSON’
}
Any help would be greatly appreciated.