Home
Support Forums

Proxi with _redirects

Hi! I’m trying proxy to another service to access an API.

My proxy (to local) in Gatbsy is:

proxy: {
    prefix: "/api",
    url: "https://almayser.ipzmarketing.com",
  },

In local run perfect

My _redirects to Netlify:
/api/* https://almayser.ipzmarketing.com/api/:splat 200

example: https://6195647ef191880008cfca11--cranky-swirles-6a5ea1.netlify.app/

In Netlify don’t create the redirect but… if change _redirects to 301:

/api/* https://almayser.ipzmarketing.com/api/:splat 301

Redirect is true, but … blocked by CORS…

Example: https://6195745a4f97a30008b060ad--cranky-swirles-6a5ea1.netlify.app/

I only add this header: "Access-Control-Allow-Origin": "*",

My domain is .es and can’t be registered through Netlify.

What could be the problem?

Hi @Alberto-ln,

I’m not seeing this problem. For example, if I visit:

https://6195647ef191880008cfca11--cranky-swirles-6a5ea1.netlify.app/api/1

I get a 404 which is similar to:

https://6195745a4f97a30008b060ad--cranky-swirles-6a5ea1.netlify.app/api/1

So, it looks like the almayser.ipzmarketing.com server is handling the request for both of these URLs.

I also checked your page and I am not seeing any call to /api/ endpoint. Could you explain how to reproduce this?

Yes sorry.

  1. Open console in browser
  2. Go to bottom page
  3. Set email
  4. Check “Acepto la política de privacidad”
  5. Submit “UNIRME”
  6. In console read log

The text box success in UI isn’t true

Thanks!

This seems to be a problem with your configuration. The API being called is this:

https://6195647ef191880008cfca11--cranky-swirles-6a5ea1.netlify.app/api/v1/subscribers/undefined/resend_confirmation_email

I don’t think undefined is expected. I’d assume that it needs to be replaced with the variable and that variable is returning undefined.

This undefined is generated because the first call fails.

The process is:

return fetch(`/api/v1/subscribers/sync`, {
      method: "POST",
      headers: {
        "content-type": "application/json",
        "x-auth-token": process.env.MAILRELAY_TOKEN,
      },
      body: JSON.stringify({
        status: "inactive",
        restore_if_deleted: true,
        email: subscriber.email,
        group_ids: groups_ids,
      }),
    }).then((response) => {
      return response.json();
    });
  }

And it’s true get id and…

  activateUser(id: number): Promise<boolean> {
    return fetch(`/api/v1/subscribers/${id}/resend_confirmation_email`, {
      method: "POST",
      headers: {
        "content-type": "application/json",
        "x-auth-token": process.env.MAILRELAY_TOKEN,
      },
    })
      .then(() => {
        return true;
      })
      .catch(() => {
        return false;
      });
  }

Launch this request but it shouldn’t, thats my fault
but i want to be sure it works before debugging. In local run perfect.

Thanks!

Simple, it’s because of this:

The sync endpoint returns the following:

As you can see, you’re not sending a valid auth token:

image

Since you’re using the auth token in frontend, you don’t need to use environment variables. You can hardcode the token in the code itself. Because even if you use environment variables, it’s not serving the purpose as anyone can see the token in the Network tab just like I did.

I’d recommend using Netlify Functions for this and use the token in there.

But if you want to absolutely use the token like that, I think you need to change the way how you’re adding the token during build. You’re using Gatsby, and it accepts variables with GATSBY_ prefix only:

1 Like

I had tried before hardcode the token but with the redirect 301 and that failed.

Now I hardcode the token and change and test with 200 and run perfect!

Thanks you so much, now i will learn how it works Netlify Functions!