TypeError: Failed to parse URL from /api/someapi
Answered
Pacific sand lance posted this in #help-forum
Pacific sand lanceOP
I'm trying to fetch something inside a webhook handler route and this happens, it's probably my directory but i really have no clue how to fix it
Code :
Directory : in the image
Code :
import { NextRequest, NextResponse } from 'next/server'
export async function POST(request) {
const xenditXCallbackToken = 'Z9A3UpJEokuMaDXTBxF2CCv8oJcXmfN2pxVmhaQRuQqCvg9t';
const xIncomingCallbackTokenHeader = request.headers.get('x-callback-token');
if(xIncomingCallbackTokenHeader === xenditXCallbackToken){
const arrRequestInput = JSON.parse(await request.text());
const {
id: _id,
external_id: _externalId,
user_id: _userId,
status: _status,
paid_amount: _paidAmount,
paid_at: _paidAt,
payment_channel: _paymentChannel,
payment_destination: _paymentDestination
} = arrRequestInput;
const lunas = "LUNAS"
try {
const updatedBody = {
_externalId,
lunas,
}
await fetch('../../updatepaystatus', {
method: 'PUT',
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify(updatedBody)
})
} catch (error) {
console.log(error)
}
return NextResponse.json({arrRequestInput});
} else {
return NextResponse.json({ message: 'Invalid token' }, { status: 403 });
}
}Directory : in the image
Answered by riský
well, you shouldnt: https://nextjs-faq.com/fetch-api-in-getserversideprops
7 Replies
are you fetching your own url?
@riský are you fetching your own url?
Pacific sand lanceOP
yeah, in my api directory aswell
well, you shouldnt: https://nextjs-faq.com/fetch-api-in-getserversideprops
Answer
Pacific sand lanceOP
any idea for the best way to do this
ah, i should move the code from the api i'm trying to fetch inside of the server component then? don't need to fetch?
Pacific sand lanceOP
yep that fixed it
thank u so much