- Newest
- Most votes
- Most comments
When handling the x-amzn-marketplace-token in a headless Next.js application, there are a few things to consider regarding POST requests.
The issue you're experiencing with your handler function might be related to how Next.js processes incoming POST requests. Here are some potential solutions:
-
Make sure your API route is properly configured to handle POST requests. In your Next.js API handler, you need to ensure that the Content-Type headers are properly set and that you're correctly parsing the incoming request body.
-
You might need to manually add CORS headers to your response. When Amazon Marketplace sends the token to your endpoint, it's making a cross-origin request. Try adding these headers to your response:
export default async function handler(req, res) { // Set CORS headers res.setHeader('Access-Control-Allow-Origin', '*'); // Or specify your allowed origins res.setHeader('Access-Control-Allow-Methods', 'POST, OPTIONS'); res.setHeader('Access-Control-Allow-Headers', 'Content-Type'); // Handle OPTIONS preflight requests if (req.method === 'OPTIONS') { return res.status(200).end(); } if (req.method !== 'POST') { return res.status(405).json({ error: 'Method Not Allowed' }); } // Process the token from the request body // ...rest of your code }
- Check how you're accessing the token in the request. Depending on how Amazon is sending the data, you might need to access it differently:
// If sent as JSON const token = req.body['x-amzn-marketplace-token']; // If sent as form data const token = req.body.get('x-amzn-marketplace-token');
- Consider using a middleware like body-parser or the built-in Next.js body parsing to ensure the request body is properly parsed before your handler function runs.
Remember that for security reasons, you should validate the token properly and avoid setting overly permissive CORS policies in production environments.
Sources
POST method not checked automatically on API Gateway CORS settings | AWS re:Post
Insecure CORS policy | Amazon Q, Detector Library
Relevant content
asked 5 years ago

I have already added the cors headers like above and this does not fix my issue as it still fails it does not read the request as a post method.