Skip to content

Obtatining x-amzn-marketplace-token in a headless nextjs App

0

Hi I am trying to migrate our existing product pages that where using a php backend to a nextjs site. Im trying to fetch the post request containing x-amzn-marketplace-token when the form sends them to the front end.

However when i try to fetch the request from my js file it stops here at the first condition. Its not reading the post request being sent from amazon saas page. Is their some setting I have to change or am i going about this wrong.

export default async function handler(req, res) { if (req.method !== 'POST') { return res.status(405).json({ error: 'Method Not Allowed' }); }

asked a year ago210 views

1 Answer
1

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:

  1. 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.

  2. 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
}
  1. 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');
  1. 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

answered a year ago

EXPERT

reviewed a year 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.

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.