0

I have a requirement to import users to auth0. To achieve this I am using auth0's bulk user import API which accepts JSON file as input.

In my lambda, I have a JSON object with all the users list. I am able to write this data to a file and send to auth0 which works just fine but

Is there a way I can directly send this JSON object as a file?

This is the working code for me -

const options = {
    uri: `${auth0Url}/api/v2/jobs/users-imports`,
    method: 'POST',
    headers: {
        Authorization: `Bearer ${this.token}`,
    },
    formData: {
        users: fs.createReadStream('/home/tushar/Documents/codes/auth-svc/test.json'),
        connection_id: 'auth0 connection id',
        upsert: 'false',
    };
    rp(options)
    .then((body) => {
        console.log('Success-', body);
    })
    .catch((err) => {
        console.log('-Error-', err);
    });

Is it possible to send without writing and sending a file?

Farhan Yaseen
  • 2,507
  • 2
  • 22
  • 37
Tushar Gaurav
  • 486
  • 4
  • 18
  • Any [Readable]Stream will suffice as a replacement stream: https://stackoverflow.com/questions/12755997/how-to-create-streams-from-string-in-node-js , https://stackoverflow.com/questions/13230487/converting-a-buffer-into-a-readablestream-in-nodejs - and I suspect a simple string would itself be sufficient. – user2864740 Jan 28 '19 at 07:32
  • @user2864740 Tried with simple string. Got error : {"statusCode":415,"error":"Unsupported Media Type","message":"Unsupported Media Type"} – Tushar Gaurav Jan 28 '19 at 07:41

1 Answers1

2

I had this exact problem - turns out you need to specify a filename in the options for the users in formData:

const options = {
  uri: `${auth0Url}/api/v2/jobs/users-imports`,
  method: 'POST',
  headers: {
      Authorization: `Bearer ${this.token}`,
  },
  formData: {
    users: {
      value: JSON.stringify(yourJsonUserVariable),
      options: {
        filename: 'export.json', 
        contentType: 'application/json'
      }
    },
      connection_id: 'auth0 connection id',
      upsert: 'false',
  };
  rp(options)
  .then((body) => {
      console.log('Success-', body);
  })
  .catch((err) => {
      console.log('-Error-', err);
  });
}
simo
  • 15,078
  • 7
  • 45
  • 59
vonazt
  • 101
  • 1
  • 5