首页 > 解决方案 > 如果我用 axios 调用 digisigner 文档 api,则会出现错误请求错误

问题描述

如果我从邮递员那里调用 DigiSigner,它工作正常,但是如果我使用 axios form nodejs 应用程序调用它,它会给出错误错误请求。

这是我的代码示例。

  const digi_signer_url = "https://api.digisigner.com/v1/documents";
    const digi_signer_options = {
        method: 'POST',
        headers: { "Content-Type": "multipart/form-data", 'Authorization': `Basic ${process.env.DIGI_SIGNER_KEY}` },
        data: {file: file},
        url: digi_signer_url,
    }

    const r = axios(digi_signer_options)

标签: node.jsaxiosdigital-signature

解决方案


这是解决方案。

const opts = {
      method: 'POST',
      url: 'https://api.digisigner.com/v1/documents',
      headers:
      {
        'cache-control': 'no-cache',
        authorization: `Basic ${process.env.DIGI_SIGNER_KEY}`,
        'content-type': 'multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW'
      },
      formData:
      {
        file:
        {
          value: fs.createReadStream(path.join(__dirname, '../../templates/sample-po.pdf')),
          options: { filename: 'sample-po.pdf', contentType: null }
        }
      }
    };

    request(opts, function (error, response, body) {
      if (error) throw new Error(error);

      if (body) {
        const document = JSON.parse(body);    
        const {document_id} = document;

        const send_to_signature = {
          method: 'POST',
          url: 'https://api.digisigner.com/v1/signature_requests',
          headers:
          {
            'cache-control': 'no-cache',
            'content-type': 'application/json',
            authorization: `Basic ${process.env.DIGI_SIGNER_KEY}`
          },
          body:
          {
            documents:
            [{
              document_id: document_id,
              subject: subject,
              message: content,
              signers:
              [{
                email: to_email,
                fields: [{ page: 0, rectangle: [0, 0, 200, 100], type: 'SIGNATURE' }]
              }]
            }]
          },
          json: true
        };

        request(send_to_signature, function (error, response, result) {
          if (error) throw new Error(error);
            console.log(result)

        });

      }

    });

推荐阅读