405 Error when form submit POST #759
Replies: 3 comments
-
I have the same problem and are looking for a solution :-( |
Beta Was this translation helpful? Give feedback.
-
simple fix allthough you need to handle security yourself than. add router.all("/api/(.*)", handleRequest); // api is clear as well before router.get("/_next/webpack-hmr", handleRequest); // Webpack content is clear
router.get("(.*)", async (ctx) => { in server/server.js such that the result looks like this router.all("/api/(.*)", handleRequest); // api is clear as well
router.get("/_next/webpack-hmr", handleRequest); // Webpack content is clear
router.get("(.*)", async (ctx) => { |
Beta Was this translation helpful? Give feedback.
-
A bit late, but I'm answering this question to help others. I believe you're getting that error because you need to add in the JWT to the request, you're getting a 405 method not allowed because it is literally not allowed. You can use the //in _app.js
function MyProvider(props) {
const app = useAppBridge();
if (typeof window !== "undefined") {
window.app = app; // saving instance of app in window variable
... // in a utils file
import { getSessionToken } from "@shopify/app-bridge-utils";
const authFetch = async (url, options = {}) => {
const newToken = await getSessionToken(window.app);
const updateOptions = (newOptions) => {
const update = { ...newOptions };
update.headers = {
...update.headers,
Authorization: `Bearer ${newToken}`,
};
return update;
};
return fetch(url, updateOptions(options));
}; // usage is like normal fetch
const response = await authFetch("/api/v1/your-route", {
method: "POST",
headers: {
Accept: "application/json",
... other headers
},
body: JSON.stringify(yourStuff),
}).then((res) => res.json()); What this function does is append a token header to all of your requests basically saying "yeah I'm from the app let me make my requests to the backend from here thanks". Hope that helps. |
Beta Was this translation helpful? Give feedback.
-
I'm trying to submit a form but get a 405 error when trying to POST to my api route. I've tried using both axios and the fetch api.
What do you think should happen?
Expect this object to be returned:
{ name: 'Success' }
- Successful POST requestWhat actually happens?
405 Error: https://snipboard.io/u8PKS6.jpg
Steps to reproduce the problem
Scaffold with Shopify CLI. Create an api endpoint and a page to call that endpoint from.
/survey/create
/api/survey
Specifications
Beta Was this translation helpful? Give feedback.
All reactions