Request with API Routes
Answered
AlphaRyze posted this in #help-forum
Hey good morning, i have a question with my code, in this case i am building a manage subscription button which from the main user page when you click manage it performs a POST request to the api route which generates a stripe billing portal link and returns it, and until that it works but every time i click the button it performs both a POST request and shortly after a GET request causing the page to load an error page, why does it perform also a GET request i am not understanding this thing. Thanks for the help in advance
import { useSelfUser } from '@/api/hooks';
import { profile } from '@/config/translations/profile';
import { useSession } from '@/utils/auth/hooks';
import { Box, Button, Text } from '@chakra-ui/react';
import { useRouter } from 'next/router';
export function ManageSubscriptionButton() {
const router = useRouter();
const user = useSelfUser();
const session = useSession();
const t = profile.useTranslations();
const redirectToCustomerPortal = async () => {
try {
const { url } = await fetch('/api/stripe/create-portal-link', {
method: 'POST',
body: JSON.stringify({ user, session }),
headers: {
'Content-Type': 'application/json',
},
});
console.log(url);
router.push(url);
} catch (error) {
if (error) return alert((error as Error).message);
}
};
return (
<>
<Button
variant="solid"
colorScheme="teal"
disabled={!user}
onClick={redirectToCustomerPortal}
>
{t.customerPortal}
</Button>
</>
);
}Answered by AlphaRyze
the solution was to change from { url } to { stripeUrl } since the code was interpreting the ORIGIN URL as the URL of stripe so it was sending me back there every time, so i just changed the name of the propriety
10 Replies
Show your network tab so we can see what its actually "GETting", also would it not make sense to have a GET request instead of a POST request in the first place since you are basically "getting" the link from your api.
@Clown Show your network tab so we can see what its actually "**GET**ting", also would it not make sense to have a GET request instead of a POST request in the first place since you are basically "getting" the link from your api.
yea cause i am posting the data to the api route to recieve back the stripe URL but when it should redirect to the stripe URL just generated it performs somehow a GET request to the same api route when it shouldn't causing a 405 error code since it's not a method allowed
Hmm i guess ok, can you show how you are returning the response?
1) is your api not using route handlers?
2) how are you printing the "GET" / "POST"?
3) If the api is using route handler how are you returning the response (this is what i wanted to see)
2) how are you printing the "GET" / "POST"?
3) If the api is using route handler how are you returning the response (this is what i wanted to see)
@Clown 1) is your api not using route handlers?
2) how are you printing the "GET" / "POST"?
3) If the api is using route handler how are you returning the response (this is what i wanted to see)
1. i am using route handlers
2. the method is being printed in the api route
3. i am returning it via res.status.json when RES is a NextResponse object
2. the method is being printed in the api route
3. i am returning it via res.status.json when RES is a NextResponse object
I tried changing from a router.route() to a windows.href.location = url; but still i didn't have any new results always the post and get result
the solution was to change from { url } to { stripeUrl } since the code was interpreting the ORIGIN URL as the URL of stripe so it was sending me back there every time, so i just changed the name of the propriety
Answer