customerio / customerio-node

A node.js client for the Customer.io REST API.
https://customer.io/docs/api/
MIT License
53 stars 35 forks source link

301 Unknown error #134

Closed vcombey closed 1 year ago

vcombey commented 1 year ago
    const client = new APIClient("********************************");
    try {
        const customers = await client.getCustomersByEmail(data.to);
    } catch (err: any) {
        console.log(err.statusCode, err.message)
    }

This code logs: 301 Unknown error

I'm using "customerio-node": "3.4.1",

I don't know what to do

asplunds commented 1 year ago

I'm getting this too. It seems the backend is trying to redirect to "https://beta-api-eu.customer.io" but the client is not following redirects. I believe this is an EU only issue.

if you log the full error you get:

body: '{"errors":[{"detail":"wrong datacenter","status":"301"}]}'

A workaround would be to use the REST api instead.

asplunds commented 1 year ago

Looking further it appears request.ts is using node https, this package has no built-in feature to follow redirects and does not follow redirects by default. You have to do it manually.

This is a pretty serious issue since if the backend wants to make a simple redirect, that could brick entire apps relying on cio. Please look into it.

mike-engel commented 1 year ago

Thanks for the info @asplunds, I have a PR ready with #135.

The real root cause here, however, is that you're not configuring the client to use the EU region (it defaults to the US). You should be creating the client like so:

const { APIClient, RegionEU } = require('customerio-node');

const client = new APIClient("********************************", { region: RegionEU });

Setting up the correct region avoids unnecessary redirects

asplunds commented 1 year ago

Thanks for the response @mike-engel, this was a mistake on my part I should've configured my client to use the EU region. I'm glad this was caught and being fixed however, since following redirects is still important regardless of region.

mike-engel commented 1 year ago

Fix released with 3.5.1. Thanks y'all