echosign CombinedDocument API [英] echosign combinedDocument api

查看:101
本文介绍了echosign CombinedDocument API的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

https://api.na1.echosign.com/api/rest/v5/agreements/ {agreementId}/combinedDocument

https://api.na1.echosign.com/api/rest/v5/agreements/{agreementId}/combinedDocument

我正在尝试从响应的正文创建文件,但是它正在创建无法打开的文件.即使文件上没有密码,它也需要密码.我认为这一定与编码/解码有关.

I am trying to create a file from the body of the response, but it is creating a file that I can't open. It requires a password even though there isn't one on the file. I think this must have something to do with the encoding / decoding.

我正在使用节点快递服务器.这是我正在使用的几行代码:

I am using a node express server. Here are the few lines of code I am using:

var request = require('request');

request({
    baseUrl: 'https://api.na1.echosign.com/api/rest/v5',
    url: '/agreements/' + req.params.id + '/combinedDocument',
    headers: {'Access-Token': process.env.ECHOSIGN_INTEGRATIONKEY}
  },
  function(error, response, body){
    if(error) {
      res.send(error);
    }
    else {
      var buf = new Buffer(body)
      res.set({
        'Content-Disposition': 'attachment; filename=test.pdf',
        'Content-Type': 'application/pdf; charset=utf-8'
       });
       res.write(buf);
       res.end();
    }
  }
);

推荐答案

这是最终在其他人偶然发现的情况下有效的方法.我认为问题在于从API返回的数据是流,并且它需要具有分块逻辑,然后进行级联以避免损坏.

This is what ended up working in the end in case somebody else stumbles across this. I think the problem was that the data being returned from the API is a stream and it needed to have the chunking logic and then get concatenated in order to avoid getting corrupted.

还包括对base64的编码,将其推送到数据库中,然后将其取回,对其进行解码,然后将其推送到浏览器.我本来不会那样做,但可以通过这种方式来测试整个周期.

Also included is encoding to base64, pushing it into a database and then getting it back, decoding it and pushing it to the browser. I wasn't going to leave it like that, but had it set up that way to test the full cycle.

router.get('/echosign/agreement/:id', function(req, res) {
  if (req.user !== 'myUserId') {
    console.log(req.user);
    res.redirect('/');
  } else {

    var request = require('request');
    var data = [];

    request({
      baseUrl: 'https://api.na1.echosign.com/api/rest/v5',
      url: '/agreements/' + req.params.id + '/combinedDocument',
      headers: {'Access-Token': process.env.ECHOSIGN_INTEGRATIONKEY}
    }).on('data', function(chunk){
        data.push(chunk);
      })
      .on('end', function(){
        data = Buffer.concat(data).toString('base64');

        client.connect(function(err) {
          if(err) {
            return console.error('could not connect to postgres', err);
          }
          client.query("UPDATE agreements SET file = '" + data + "' WHERE agreementid = '" + req.params.id + "' RETURNING agreement, file", function(err, result) {
            if(err) {
              return console.log(result, err);
            }
            client.end();
            res.set({
              'Content-Type': 'application/pdf;charset=UTF-8',
              'Content-Disposition': "inline; filename='" + result.rows[0].agreement.name + ".pdf'"
            });
            res.send(new Buffer(result.rows[0].file, 'base64'));
          });
        });
      });
    }
});

这篇关于echosign CombinedDocument API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆