【问题标题】:How to send JSON object as file如何将 JSON 对象作为文件发送
【发布时间】:2019-06-21 04:09:29
【问题描述】:

我需要将用户导入 auth0。为此,我使用了 auth0 的批量用户导入 API,它接受 JSON 文件作为输入。

在我的 lambda 中,我有一个包含所有用户列表的 JSON 对象。我可以将这些数据写入文件并发送到 auth0,它工作得很好,但是

有没有办法可以直接将此 JSON 对象作为文件发送?

这是我的工作代码 -

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);
    });

可以不写不发送文件就发送吗?

【问题讨论】:

标签: node.js lambda auth0


【解决方案1】:

我遇到了这个确切的问题 - 原来你需要在 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);
  });
}

【讨论】:

    猜你喜欢
    • 2013-02-09
    • 2021-01-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多