App reviews, troubleshooting, and recommendations
Hello
Trying to do a POST request from web/components/form and it returns 302, when using appUseQuery hook works.
Tutorial source: https://shopify.dev/apps/getting-started/build-app-example/backend
web/components/form.jsx
useCallback(() => {
(async() => {
console.log('INIT REQUEST');
const response = await fetch('/api/myapi', JSON.stringify(), {
headers: {
'Content-Type': 'application/json'
}
});
if (response.ok) {
console.log('RESPONSE OK');
const data = await response.json();
console.log('response', data);
}
})()
return { status: "success" };
}, []);
middleware/dashboard.js
export default function applyDashboardMiddleware(app) {
console.log('APPLIED dashboard');
app.post("/api/myapi", async(req, res) => {
console.log('DASHBOARD MIDDLEWARE', req.body);
const response = await fetch('https://mywebsite.com', {
method: 'POST',
body: req.body,
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
Accept: 'application/json',
}
});
console.log('response', response);
res.status(200).send(JSON.stringify({ success: true }));
// res.status(200).send(response);
});
}
index.js
...
app.use(express.json());
applyDashboardMiddleware(app);
...
Thanks,
Solved! Go to the solution
This is an accepted solution.
Solution: To use authenticated fetch useAuthenticatedFetch hook from nodejs boilerplate shopify.
This is an accepted solution.
Solution: To use authenticated fetch useAuthenticatedFetch hook from nodejs boilerplate shopify.
2m ago Learn the essential skills to navigate the Shopify admin with confidence. T...
By Shopify Feb 12, 2025Learn how to expand your operations internationally with Shopify Academy’s learning path...
By Shopify Feb 4, 2025Hey Community, happy February! Looking back to January, we kicked off the year with 8....
By JasonH Feb 3, 2025