Error when Calling Management Endpoint in Get User Script

Overview

Running the Management API GET users-by-email endpoint with axios in the Get Users script throws the error: “Error fetching user from API: unexpected end of file”. A possible cause for this is using a non-URI formatted email in the request.

Cause

For example, the following code can return a 400 error which indicates failure due to invalid syntax such as using a non-URI formatted email.

const userResponse = await axios.get(`https://test-tenant.us.auth0.com/api/v2/users-by-email?email=${userEmail}`, {
    headers: {
    'Accept': 'application/json',
    'Accept-Encoding': 'gzip,deflate,compress',
    'Authorization': `Bearer ${managementAPI}`
    }
 });

Solution

To make sure userEmail is in the right format:

  1. Add the following to convert into URI formatted email:

    const userEmail = encodeURIComponent(email);

  2. Or, Add 'Accept-Encoding': 'gzip,deflate,compress' to the header.