Node/Express , react POST request returns 302 status

Solved

Node/Express , react POST request returns 302 status

sirev
Shopify Partner
15 4 3

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,

Accepted Solution (1)

sirev
Shopify Partner
15 4 3

This is an accepted solution.

Solution: To use authenticated fetch useAuthenticatedFetch hook from nodejs boilerplate shopify.

View solution in original post

Reply 1 (1)

sirev
Shopify Partner
15 4 3

This is an accepted solution.

Solution: To use authenticated fetch useAuthenticatedFetch hook from nodejs boilerplate shopify.