Bulk User Imports, Node js example does not work

Hi,

I am following this guide right here, but the Node example does not seem to work: Bulk User Imports

The exact code I’m trying to run (with sensitive bits omitted):

var axios = require("axios").default;
const url = "https://XXX/api/v2/jobs/users-imports";
const token = "XXX";
const connectionId = "XXX";
const filename = "users.json"

var options = {
  method: "POST",
  url,
  headers: {
    authorization: `Bearer ${token}`,
    "content-type": "multipart/form-data; boundary=---011000010111000001101001",
  },
  data: `-----011000010111000001101001\r\nContent-Disposition: form-data; name="users"; filename="${filename}"\r\nContent-Type: text/json\r\n\r\n\r\n-----011000010111000001101001\r\nContent-Disposition: form-data; name="connection_id"\r\n\r\n${connectionId}\r\n-----011000010111000001101001\r\nContent-Disposition: form-data; name="external_id"\r\n\r\n\r\n-----011000010111000001101001--\r\n`,
};

axios
  .request(options)
  .then(function (response) {
    console.log(response.data);
  })
  .catch(function (error) {
    console.error(error);
  });

The error I’m currently getting:

data: {
  statusCode: 400,
  error: 'Bad Request',
  message: 'Users file must not be empty',
  errorCode: 'operation_not_supported'
}

Anyone can help me with this? (And maybe the docs could be updated with a working example?)

p.s. I have already tried to modify in different ways (following posts on here and stack overflow), and nothing has worked, always getting some kind of error…

Have you ensured the path of the users.json file you are using here is in the path the NodeJS script is looking for it in, and that the users.json file is not an empty file? There’s a basic example of the users.json file at Bulk User Import Database Schema and Examples you can use for testing, with more complex examples throughout that same page. Hope this helps.

1 Like

The file with the upload code is in the same directory as users.json. And users.json is not an empty file…

1 Like

Does it give the same behavior/error message with the sample file at the link shared above, specifically copying and pasting Bulk User Import Database Schema and Examples sample data into a users.json file and using this as the source of users for the upload script?

  • If it does give the same error, then you may want to double check the path you are using for referencing the file, as it would most likely be that it simply isn’t seeing the file.
  • If it does not give the same error and uploads correctly, can you try copying the data from the original non-working users.json file and pasting and overwriting the data in the file that works?

This will then reveal if it is a file data error if this doesn’t work, or an actual saved file format error if this does work, as is sometimes (depending on your OS) where a file editor may use a file encoding that doesn’t quite work as expected with an HTTP POST.

Hope this helps.

2 Likes

How else would I refer to the file? Shouldn’t it be clear or not my the code above if it would see the file or not? (Which is basically the same as the Auth0 example, I just set it to a variable instead of having it inline).

Hi, I got the same error, using python.
I use full path to json file and relative path and both the same error, if I exec from curl works fine. so the file is well json formed.
Any Help?

I’m having the same issue. Have tried relative and absolute paths. JSON file is well formed.

Can you try the following script? You’ll need to npm i form-data.

const axios = require("axios")
const FormData = require('form-data')
const fs = require('fs')

const FILENAME = 'example.json'
const AUTH0_DOMAIN = 'example.auth0.com'
const TOKEN = 'REPLACE_WITH_TOKEN'

const run = async () => {
  const formData = new FormData()
  formData.append("connection_id", "con_abcdef1234") // Replace with correct connection ID
  formData.append("users", fs.createReadStream(FILENAME))

  try {
    const res = await axios.post(`https://${AUTH0_DOMAIN}/api/v2/jobs/users-imports`, formData, {
      headers: Object.assign({}, formData.getHeaders(), {
        authorization: `Bearer ${TOKEN}`
      })
    })
    console.log(res.data)
  }
  catch (err) {
    console.log(err)
  }
}

run()
2 Likes

Thanks for helping on this one Thameera!

Hi, I’m seeing this issue in python, and the input file is not empty. Is the python snippet provided in the guide out of date? Guide: Bulk User Imports.

Hi there could someone help with my question? Thanks! I’d also like to add that I ran the curl command with my own parameters, and it worked fine. It seems to be the python snippet.

Hi, same applies to the C# snippet. It doesn’t work the way it seems to be setup. Saw some examples online that use RestRequest and they added the file with ‘request.AddFile()’ method so maybe something is missing in those code examples for User imports?

@ciem The following code seems to work fine, but my Python is rusty so don’t consider this production-ready code:

import requests

TOKEN='REPLACE_WITH_TOKEN'

url = 'https://TENANT.auth0.com/api/v2/jobs/users-imports'
headers = { 'Authorization': 'Bearer ' + TOKEN }

payload = ( ('users', open('tmp.json', 'rb')) , ('connection_id', (None, 'con_abcdef1234')) )

r = requests.post(url, headers=headers, files=payload)
print(r.text)  # You may need to parse the JSON in r.text
3 Likes

Thanks for helping on this one Thameera!

Hi Thameera. I’m having the same issue. any chance you have a PHP example of your solution?

1 Like

This worked, thank you so much.

1 Like

This code worked for me,

const formData = new FormData();
formData.append(“connection_id”, process.env.AUTH0_CONNECTION_ID);
formData.append(“users”, fs.createReadStream(filePath));
formData.append(“external_id”, externalId);

    const headers = formData.getHeaders();
    headers.authorization = AUTH0_HEADERS.authorization;
    const response = await axios.post(`${process.env.AUTH0_API_BASE_URL}api/v2/jobs/users-imports`, formData, {
        headers,
    });

Hey there, have you found any solution in PHP? I m having the same issue

The following part is working for PHP in case someone needs it

$cf = new CURLFile(USERS_IMPORT_FILE);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, CONNECTION_URL);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, [“users” => $cf, “connection_id” => CONNECTION_ID]);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$headers[ ] = "Authorization: Bearer ".MGMT_API_ACCESS_TOKEN;
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

$response = curl_exec($ch);
$err = curl_error($ch);
curl_close($ch);
if ($err) {
    echo "cURL Error #:" . $err;
} else {
    echo $response;
}

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.